Introduction
Among the many changes to .NET in 2.0, there was an overhaul of the classes for sending E-Mail. For those of you unfamiliar with the new System.Net.Mail classes, or those of you trying to send mail for the first time, this article is for you! I’ll be specifically targeting sending mail through Exchange, so you may need to make some small changes if you’re using another type of server.
What has changed?
The most obvious change to sending mail in VS 2005 is that the classes are located in a new namespace, System.Net.Mail instead of System.Web.Mail. The MailMessage class itself has received a bit of an overhaul, as well as the addition of the MailAddress class (this class caused me a bit of a headache initially which I’ll discuss later). Overall, the class has become more of what I’d expect out of .NET. Properties are easier to set and the object is more complex and more functional. I go into some specifics below.
Creating a Message – The MailMessage class
To start, let’s cover the creation of a simple message, and then I’ll cover how the new class differs from the 1.1 version.
Dim oMessage As New MailMessage()
oMessage.Subject = "This is my subject"
oMessage.Body = "<html><body><a href=""http://geekswithblogs.net/cubeberg"">Check out my blog!</a></html></body>"
oMessage.IsBodyHtml = True
oMessage.From = New MailAddress("myaddress@gmail.com")
oMessage.To.Add(New MailAddress("youraddress@gmail.com"))
oMessage.Priority = MailPriority.High
This is a pretty simple example as there are several other properties that we haven’t used such as Attachments, AlternateViews, ReplyTo, etc, but for most developers, these are the properties that you’ll deal with on a routine basis. In all reality, this wasn’t a difficult class to start with, but they’ve taken a simple class and added even more functionality (something you’ll see more of if you’re upgrading to 2.0).
I’ll cover the new MailAddress class in a second, but for those of you who are familiar with the Web.Mail classes, you’ll notice that From, To, BCC, etc. are no longer simple strings. The new IsBodyHtml property is a nice addition and a simple way to specify that your body contains HTML and not plain text.
The MailAddress class
As I’ve mentioned above, the new MailMessage class no longer accepts simple strings for email addresses. Microsoft has introduced the MailAddress and MailAddressCollection classes. They provide some additional validation, as well as simplifying some functionality. Removing and adding addresses at run time no longer requires parsing of strings. We can now work with a strongly-typed collection that is more indicative of the .NET environment. You may also want to look at the DisplayName property which I’ve found very useful.
When I upgraded my first application from 1.1 to 2.0, I noticed the errors about Web.Mail being obsolete. So I changed my code to use the new classes. When I started testing the application, I was running into issues specifying addresses for my messages. I was working on an Intranet application and was only specifying the username and no host for the address. The MailAddress class requires that a host be specified. I solved the issue by adding our exchange server after the windows username - “@exchange.mycompany”.
Sending the message – The SmtpClient class
Once again, sending an email is a simple thing. Below you’ll see an example.
Dim client As New SmtpClient("exchange.mycompany")
client.Send(oMessage)
For those of you who may be running into authentication issues, there is an additional property on the SmtpClient class that you’ll need to specify. In my situation, I use the credentials of the current windows user, but you can also specify another credential type if you need to.
client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
You may also notice things have changed slightly from 1.1. We’re using the SmtpClient class instead of the SmtpMail class. For me, there’s no significant change, but you’ll notice several new properties on the new class, and the underlying technology seems to have changed. The SmtpMail class indicates that it uses CDOSYS, while the SmtpClient class simply indicates that it’s using the SMTP protocol.
Where can I expand from here?
While the applications for sending emails from within an application are endless, here are a few ideas for you to expand on this class and add some functionality to your applications. Create a class that constructs a pre-formatted email from XML or a Database. Use emails for sending notifications of errors within your application, rather than relying on your users to notify you of problems or an admin to check errors logged in a database.