Random thoughts about everything and nothing

Sending mail in 2.0 – Part 1

leave a comment »

The .NET framework 2.0 has been loaded with interesting new features and classes. Beside these completely new items, some pre-existing classes have been buffed and moved to a new namespace. A good example is the System.Web.Mail, used to construct and send SMTP Messages.

Welcome to System.Net.Mail

Before proceeding, let’s take a look at the internal plumping before digging into the new features. Unlike the framework 1.X version, the System.Net.Mail libraries have been built up without relying on any interop interaction…exit CDO/CDO.SYS (Collaboration Data Objects for Windows 2000) we might say. From now on, you simple need the .NET framework 2.0 installed.

Let’s start with a very familiar piece of code for sending a message.

MailMessage mail = new MailMessage();

mail.From = new
MailAddress(“mike@hatmail.com”, “Mike Doe”);


mail.Subject = “Interesting…”;
mail.Body = “Sending mail in 2.0”;

SmtpClient smtp = new SmtpClient(“”);


Notice the basic outline of sending a message hasn’t changed. The MailMessage class is the still the foundation. It deals with creating and managing the email message. All other classes will somehow interact with this class. You will observe the first big change with the MailAddress class, used for creating email addresses.

Since the To, Cc and Bcc properties are collection we can add multiple addresses by simple calling the .Add() method. No more need for the “;” separated string when assigning multiple recipients.

Pretty similar, I can use the Attachments property to include one or more attachments to my message. These can be created from streams or file paths. Have a look at this snippet:

MailMessage mail = new MailMessage();

mail.From = new
mail.Subject = “Attack of the attachments”;
mail.Body = “Do you see

mail.Attachments.Add(new Attachment(@”c:\proj\doc1.txt”));
mail.Attachments.Add(new Attachment(@”c:\proj\doc2.txt”));

As mentioned, next to the files stored on disk you can use streams too, more on that next time…

Return to sender

One thing you would like to know when sending emails: who didn’t receive my mail. This will be handled through the SmtpFailedRecipientException and SmtpFailedRecipientsException.

Here’s the source to demonstrate:

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.From = new
mail.Subject = “Urgent”;
mail.Body = “Hope you get this message!”;
SmtpClient smtp = new

catch (System.Net.Mail.SmtpFailedRecipientsException exRecipients)

catch (System.Net.Mail.SmtpFailedRecipientException exRecipient)


My server will only allow relay mails to the “hatmail.com” domain, as such the first two addresses will cause an exception.

When thrown, I can now easily identity the faulty addresses by iterating the InnerExceptions property. Each item containing the FailedRecipient address. How’s that for simplicity!


I won’t be far off be saying that a lot of today’s applications use some sort of email (notification) .

The System.Net.Mail namespace delivers an upgraded platform for sending Smtp messages with some interesting new features. Even I you don’t need to more advanced classes in this namespace, you will still enjoy the ease of managing your mail messages. Although the explicit dependency on the CDO.sys has been removed, it is still dependent on Enterprise services (COM+).


Written by Michael

March 2, 2006 at 9:15 pm

Posted in .NET, Article

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: