Sending E-Mails in .NET effectively

13 January, 2010 | | 0 comments |

Share |
The intent of this blog is to enable you to send E-mails leveraging all the features available in .Net Framework. Probably, if you already know how to send an E-mail in .Net then hold on and keep reading because you'll learn some other not-so-usually-known aspects/properties while sending an E-mail. You should be having your IIS configured with your SMTP details on your web-server OR should mention them explicitly in the [web.config] file before sending your first E-Mail. The former option is possible only in fraction of the cases since developers do not have access to the IIS Console on the web-server and hence the latter is the only option which a majority of developers use. The [web.config] method to declare the SMTP details has been included in this chapter. Note that the third option of declaring the SMTP details explicitly in the CODE is also available but as such it is vulnerable to exploits and it also contradicts the very concept of ENCAPSULATION.

The classes and methods for sending an E-mail are defined in the "System.Net.Mail" namespace and are accessible in both Windows and Web Forms(i.e. ASP.Net). The three main classes in use from the namespace while sending an E-mail are the MailAddress, MailMessage and SmtpClient.

The following are the attributes which will come into play while sending an E-Mail :

  • To address
  • CC address
  • Bcc address
  • From address
  • Body of the mail
  • Subject of the mail
  • Priority of the mail
  • Attachments (if any)
  • Does the body have HTML(as in case of Newsletters) ?
  • ReplyTo Address

Make sure the the requisite namespace [System.Net.Mail] is referenced in your code-behind.

CODE Snippet :

  2. try
  3. {
  4.     MailAddress to = new MailAddress("", "Mr. RECEIVER");
  5.     MailAddress from = new MailAddress("", "Mr. SENDER");
  6.     MailAddress cc = new MailAddress("", "Mr. CC");
  7.     MailAddress bcc = new MailAddress("", "Mr. Bcc");
  8.     MailAddress rto = new MailAddress("", "DO NOT REPLY");
  10.     MailMessage mm = new MailMessage(to, from);
  12.     mm.CC.Add(cc);
  13.     mm.Bcc.Add(bcc);
  15.     mm.Subject = "";
  16.     mm.Body = "";
  18.     mm.ReplyTo = rto;  //Specifies the E-Mail address of the SENDER which will be seen if the RECEIVER decides to reply to the mail
  19.     mm.IsBodyHtml = true; // "true" if your body has some Html content. DEFAULT is "false".
  20.     mm.Priority = MailPriority.Normal; //Possible values are "High/Normal/Low" depending upon the priority required. DEFAULT is "Normal"
  22.     mm.Attachments.Add(new Attachment(Server.MapPath("./files/image.jpg")));
  24.     SmtpClient sc = new SmtpClient();
  26.     sc.Send(mm);
  27. }
  28. catch (Exception ex)
  29. {
  30.     Response.Write("Exception : " + ex);
  31. }

[web.config] CODE Snippet :

Sending mails in .Net - 2.JPG

You can also store some requisite variables like the recipient's address, sender's address, signature or say any values in [web.config] for access at runtime.

[web.config] CODE Snippet :

Sending mails in .Net - 1.JPG

To access this global variable from CODE-BEHIND:

  2. String from = System.Configuration.ConfigurationSettings.AppSettings.("FromAddress");

Post a Comment