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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 22
    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


       · hi, this is really great,but dear friend can u please fwd. me the sql script for...
       · Hi, my friend over there. Don't worry. I'll now email you the sql script as soon as...
       · Hi, when I try to email the whole database to you. I've gotten a 'System Withdraw'...
       · it works fine on local machin but didnt work on severgives error:A...
     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek