.NET

Sending Emails from ASP.NET

By Dino Esposito, July 14, 2008

The secret sauce is System.Net.Mail namespace.

A common task that most ASP.NET applications share is sending notification emails to registered users for application specific events and to the site administrator when something goes wrong. But how would send emails from within an ASP.NET application? The task is not different for the ASP.NET platform and requires the same classes and techniques you would use from within, say, a desktop application. All the machinery is in the System.Net.Mail namespace.

The first thing you do is preparing a mail message. The class that represents a mail message is MailMessage. The constructor takes up to four parameters: the sender's address, the receiver's address, the subject line of the message, and the body. There are four different constructors available and you can choose to specify these arguments (at the very minimum you indicate sender and receiver) as plain strings or through instances of a container class such as MailAddress.

A MailMessage class can be further configured to specify encoding, alternate views of the message such as HTML, attachments and blind carbon copies. Is it possible to specify multiple receivers? You bet.

The MailMessage class provides properties like To, CC, and Bcc. These properties are implemented as instances of the MailMessageCollection class and let you add as many addresses as you need.

To set the subject and the body of the message you use ad hoc properties at any time. You can also set these properties through one of the constructors.

msg.Subject = theSubject;
msg.Body = theBody;

You can populate the Body property with any text you like. To send an attachment, instead, you need to resort to the Attachments collection. First, you define an Attachment object and make it point to a local file, a string or a stream.

You specify the content of an attachment by using the content type property on the constructor. The content type parameter indicates the MIME type header of the attachment.
In addition, you can add time stamp information for the file. You do this via the ContentDisposition property on the Attachment class.

Finally, to add the file to the message, you use the Addmethod and the Attachments collection.

msg.Attachments.Add(data);

The final step consists in the physical submission of the message to an SMTP server. The SmtpClient class receives the address of the SMTP server through the constructor and sends the message via its method Send.

SmtpClient mail = new SmtpClient("your-smtp-server");
mail.Send(msg);

The SmtpClient class also supports credentials to be used when the server requires the client to authenticate before accepting email on the client's behalf.

mail.Credentials = CredentialCache.DefaultNetworkCredentials;

Credentials can also be specified using the web.config file. In particular, you use the <mailSettings>element. Note that any information stored in the configuration file gets overridden once you use the Credentials property programmatically.

The Send method works synchronously, but an asynchronous version also exists. The method SendAsync sends the message and returns. The SendCompleted event is raised when the asynchronous operation completes. In an ASP.NET scenario, though, the asynchronous method is arguably an ideal solution. In ASP.NET page you might want to send all messages synchronously and return an updated user interface when you're done. The classes in the System.Net.Mail namespace offer a simple and effective programming model for sending emails programmatically.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!