November 23, 2014
Hot Topics:

Send Mails from within a .NET 2.0 Application

  • June 10, 2005
  • By Thiru Thangarathinam
  • Send Email »
  • More Articles »

Most enterprise applications today need to send mail. This means the platform should provide the necessary support for applications to send these mails. In the case of .NET applications, .NET provides excellent support for sending e-mails through a set of intuitive classes. This article introduces the new ways of sending mails through the System.Net.Mail namespace by showing examples. You also will see the steps involved in sending attachments as part of the mails, as well as sending mails to multiple recipients in the form of CC and BCC lists. Finally, it explains the use of XML and XSL in formatting the bodies of HTML-based mails.

.NET 2.0 Mail Functions

Microsoft made some interesting changes in how you can send e-mail in version 2.0 of the .NET Framework. With .NET 1.x versions, you had to utilize the classes contained in the System.Web.Mail namespace for sending mails. Now, with the release of .NET 2.0, the System.Web.Mail namespace is obsolete and its functionality is now available in the System.Net.Mail namespace. This is a welcome change because the mail functionality was intended to be used for all .NET applications and should not have been under System.Web assembly.

In addition to moving the functionality under the System.Net.Mail namespace, the .NET team also completely rebuilt the implementation of SMTP functionality. There are new classes, properties, and methods that provide an elegant and clean way of performing operations related to sending mails. This long list of new enhancements includes quality improvements, different methods, and asynchronous support out of the box.

Now that you have a general understanding of the support provided by .NET 2.0 for sending mails, let us dive deep into the actual classes contained in the System.Net.Mail namespace.

System.Net.Mail Namespace

The System.Net.Mail namespace contains all the classes required for sending mail from within a .NET application. The classes contained in this namespace work a bit differently from the classes in the Web namespace. Table 1 describes the classes contained in the System.Net.Mail namespace.

Table 1. Important Classes in the System.Net.Mail Namespace

ClassDescription
Attachment Represents an attachment that is sent with a mail and is used in conjunction with the MailMessage class
MailAddress Represents the address of the sender or recipient (To, CC, and BCC)
MailMessage Represents an e-mail message that can be sent using the SmtpClient class and exposes properties such as From, To, CC, BCC, Attachments, Subject, and Body to formulate the message's contents
SmtpClient Allows applications to send emails using the SMTP
SmtpException Represents the exception that is thrown when the SmtpClient is not able to send the message

Of the classes that Table 1 lists, MailMessage and SmtpClient are the two core classes that you need to work with to send the simplest of mails.

Implementation

The SmtpClient class handles the actual sending of e-mail. For a simple mail, you can directly pass in the To address, from, subject, and body of the mail to one of the SmtpClient's overloaded send methods. However, this approach works only for simple mails that don't need any advanced formatting, which is normally provided by the MailMessage class.

To have complete control over the mail message, you need to create an instance of the MailMessage class and set its properties to appropriate values. For the purposes of this example, create a Visual C# Class Library project named MailServer using Visual Studio 2005. Once the project is created, rename the default class to MailService. To the MailService class, add a method named SendMail and modify its code to look as follows (an example of how to use the MailMessage class to create a mail message and then send it using the SmtpClient object):

public void SendMail(string from, string to,
                     string subject, string body)
{
   string mailServerName = "smtp.test.com";
   try
   {
      //MailMessage represents the e-mail being sent
      using (MailMessage message = new MailMessage(from,
             to, subject, body))
      {
         message.IsBodyHtml = true;
         SmtpClient mailClient = new SmtpClient();
         mailClient.Host = mailServerName;
         mailClient.UseDefaultCredentials = true;
         mailClient.DeliveryMethod =
            SmtpDeliveryMethod.PickupDirectoryFromIis;
         //Send delivers the message to the mail server
   mailClient.Send(message);
      }
   }
   catch (SmtpException ex)
   {
      throw new ApplicationException
         ("SmtpException has oCCured: " + ex.Message);
   }
   catch (Exception ex)
   {
      throw ex;
   }
}

In the above code, at the time of creating an instance of the MailMessage class, you pass in the From, To, Subject, and body of the message as arguments. Then, you set the IsBodyHtml property to true to indicate the type of mail you want to send. After that, you create an instance of the SmtpClient object and then set its properties, such as Host, UseDefaultCredentials, and DeliveryMethod, to appropriate values. Finally, you send the mail using the Send method, which sends out the mail in a synchronous manner. Note that at the time of sending a mail, if an SMTP exception occurs, it will be handled in the SmtpException catch block. All other exceptions are caught by the generic Exception block. Also note that in the above method, you directly set the To property of the MailMessage object to the supplied value. If you are sending mails to multiple recipients, you need to create that many instances of MailAddress objects and add them to the MailMessage.To property, which represents a collection of MailAddressCollection objects.

Now that you have created the SendMail method, it's time to call that method from a client application.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel