ASP.NET
  Home arrow ASP.NET arrow Page 3 - Creating Your Own Online E-mail System in ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Creating Your Own Online E-mail System in ASP.NET 2.0
By: Xianzhong Zhu
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    2007-06-06

    Table of Contents:
  • Creating Your Own Online E-mail System in ASP.NET 2.0
  • Dissecting the Key Techniques: Interface Design
  • Dissecting the Key Techniques: Sending Email
  • Using SendMail

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating Your Own Online E-mail System in ASP.NET 2.0 - Dissecting the Key Techniques: Sending Email


    (Page 3 of 4 )

    2. Sending emails

    The whole process of sending email is rather complicated especially when the email contains attachments. Let's examine the detailed steps in terms of the three scenarios mentioned above, as follows:

    1) Obtain the mail address of the sender, i.e. get the address the user has configured in the profile. The following gives the crucial code snippet associated with this:

     

    string from =((WebMailProfile)HttpContext.Current.Application["WebMailProfile"]).Email;

    MailMessage mailMsg = new MailMessage();

    mailMsg.From = new MailAddress(from);

     

    2) Add the addresses of the receivers. Since there may be more than one address receiving the e-mail, you need to get the address of every recipient from the string containing these addresses, and append them one by one to the To property of the MailMessage-mailMsg object:

     

    string split = ";";

    string[] toList = To.Text.Trim().Split(split.ToCharArray());

    for(int i = 0; i < toList.Length; i++)

    {

      mailMsg.To.Add(toList[i].Trim());

    }

     

    3) Add the CopyTo (i.e. CC) addresses. There may be more than one of this kind of address; your approach should be similar to the one taken above: 

     

    string[] ccList = CC.Text.Trim().Split(split.ToCharArray());

    for(int i = 0; i < ccList.Length; i++){

      if(ccList[i].Trim().Length > 0) {

        mailMsg.CC.Add(ccList[i].Trim());

    }

    }

     

    4) Add the topic of the mail and give the appropriate coding algorithm:

     

    mailMsg.Subject = Title.Text.Trim();

    mailMsg.SubjectEncoding = Encoding.UTF8;

     

    5) Append the body of the e-mail and also specify the coding methods:

     

    mailMsg.Body = Body.Text;

    mailMsg.BodyEncoding = Encoding.UTF8;

     

    6) Specify the format of the email as HTML by default:

     

    mailMsg.IsBodyHtml = HtmlCB.Checked;

     

    7) Add the attachments. Here the reason and the operation are just like those of steps 2 or 3 above, so we simply list the relevant code below:

     

    HttpFileCollection fileList = HttpContext.Current.Request.Files;

    for(int i = 0; i < fileList.Count; i++)

    { ///adding each attachment

      HttpPostedFile file = fileList[i];

      if(file.FileName.Length <= 0 || file.ContentLength <= 0)

      {break;}

      Attachment attachment = new Attachment(file.FileName);

      mailMsg.Attachments.Add(attachment);

      nContain += file.ContentLength;

    }

     

    8) Start sending your e-mail. First, call the SendMail method of the Mail class (the wrapper of the mailing related operation for simplification). Second, use the SaveAsMail method of the Mail class to persist the e-mails sent into the Sender mailbox:

     

    IMail mail = new Mail();

    mail.SendMail(mailMsg);

    ///save the mails to send

    int nMailID = mail.SaveAsMail(mailMsg.Subject,mailMsg.Body,from, To.Text.Trim(),CC.Text.Trim(),mailMsg.IsBodyHtml,

      nContain,mailMsg.Attachments.Count > 0 ? true : false);

     

    9) If there are attachments for the e-mails, upload them to the hard disk of the server, and finally store information for these attachments in the database. This function is accomplished through the following:

     

    for(int i = 0; i < fileList.Count; i++)

    { ///add each attachment

      HttpPostedFile file = fileList[i];

      if(file.FileName.Length <= 0 || file.ContentLength <= 0)

      {break;}

      /// store these attachments into the hard disk

      file.SaveAs(MapPath("MailAttachments/" + Path.GetFileName(file.FileName)));

      /// save the attachments

      mail.SaveAsMailAttachment(

        Path.GetFileName(file.FileName),

        "MailAttachments/" + Path.GetFileName(file.FileName),

        file.ContentType,

        file.ContentLength,

        nMailID);

    }

     

    More ASP.NET Articles
    More By Xianzhong Zhu


     

    ASP.NET ARTICLES

    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...
    - Back-end Management Tasks for an ASP.NET AJA...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway