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.
MailAddress toAddress = new MailAddress(to); MailAddress fromAddress = new MailAddress(from);
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.
MailMessage msg = new MailMessage(fromAddress, toAddress); msg.To.Add(address1); msg.CC.Add(address2);
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.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
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.
ContentDisposition disp = data.ContentDisposition; disp.CreationDate = GetCreationTime(file);
Finally, to add the file to the message, you use the Addmethod and the Attachments collection.
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.