Chris B's Blog

My Blog about Hobby Electronics


News


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.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Comments

Gravatar # re: Sending an Email in Visual Studio 2005 – The System.Net.Mail.MailMessage class
Posted by David R on 5/20/2006 3:10 PM
Just wondering something....I made a program thats supposed to run from computers that belong to students. Ive included a smal BUG REPORT part that uses this SMTP class. The student will enter his email and then the commentary or suggestion in the textbox I provided. Im confused in which type of send method to use. I need the message to be sent to me regardless of the network or internet configuration of the computer. Im currently using as smtpclient my own computer account of outlook (smtp.adelphia.net) and my username and password as credentials. Isnt there a way to make the smtp client determine de smtp server on its own...so that instead of using credentials of my outlook account it can be delivered to my hotmail account automatically without need of using credentials??
Gravatar # re: Sending an Email in Visual Studio 2005 – The System.Net.Mail.MailMessage class
Posted by ramesh on 12/19/2007 12:54 AM
Failure sending mail.


MailMessage oMessage = new MailMessage();

oMessage.Subject = "This is my subject";

oMessage.Body = "<html><body><a href=\"http://geekswithblogs.net/cubeberg\">Check out my blog!</html></body>";

oMessage.IsBodyHtml = true;

oMessage.From = new MailAddress("ramesh.by@kengroup.co.in");

oMessage.To.Add(new MailAddress("ramesh.by@kengroup.co.in"));

oMessage.Priority = MailPriority.High;



SmtpClient client = new SmtpClient("localhost");

client.Send(oMessage);

client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;


Gravatar # re: Sending an Email in Visual Studio 2005 – The System.Net.Mail.MailMessage class
Posted by VK on 4/19/2008 12:39 AM
What about regular expressions that can be used for email strings? What is the best email ID that can be used?
Gravatar # re: Sending an Email in Visual Studio 2005 – The System.Net.Mail.MailMessage class
Posted by Chad Horton on 7/16/2008 7:52 AM
This is great info! I am a beginning .net programmer so this was a nice find. I do have one question though...

Instead of sending the message from the back end of the program... Is it possible to create a new message using Outlook 2003 on the clients computer? Our Exchange server is setup to only allow email like this from any IP address we add to a "allow list". I have been searching around on the internet the past few days with no luck.

Any help is greatly appreciated!
Gravatar # re: Sending an Email in Visual Studio 2005 – The System.Net.Mail.MailMessage class
Posted by Patrick Horn on 8/5/2008 4:48 PM
Is there a limit to how complicated the HTML can be? I am attempting to send the source from a html web page (http://www.pfwebservice.com/Campaigns/Newsletters/NL_sample_4.html) and it does come through a machine running Outlook 2000, but is totally blank on another machine running Outlook Express and my Web based Sbcglobal.net account. Simpler examples of HTML code do come through to all email accounts.

Gravatar # re: Sending an Email in Visual Studio 2005 – The System.Net.Mail.MailMessage class
Posted by mahendra on 11/12/2008 3:43 AM

thanks for teaching email in visual 2005

Gravatar # re: Sending an Email in Visual Studio 2005 – The System.Net.Mail.MailMessage class
Posted by Pradeep yadav on 4/17/2009 7:29 AM
Hi ,

I m using following code for sending mail
String mailtxt = "";
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
mm.From = new MailAddress("prayadav@mycompany.com");
mm.To.Add(new MailAddress("ajyadav@mycompany.com"));
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message

mm.Subject = "Test Mail through Web Service";
mailtxt = "<font face='verdana' color='#FF9900'>" + "Hi Ajeet," + "</font><br><br>";

mailtxt = mailtxt + "<font face='verdana' color='#FF0000'>" + "Good Day." + "</font><br><br>";

mailtxt = mailtxt + "<font face='verdana' color='#008080'>" + "May today be filled with sunshine and smile, laughter and love." + "</font><br><br>";

mailtxt = mailtxt + "<font face='verdana' color='#0000FF'>Cheers!" + "<br><br>";
mm.Body = mailtxt;
// SmtpClient clnt = new SmtpClient("exchange.deloitte", 25);
SmtpClient clnt = new SmtpClient("mycompany.com", 25);
clnt.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
clnt.Send(mm);

But it gives me error
No connection could be made because the target machine actively refused

Can you help me please.


Gravatar # re: Sending an Email in Visual Studio 2005 – The System.Net.Mail.MailMessage class
Posted by cubeberg on 4/17/2009 7:43 AM
Pradeep,
Looks like the server you've specified in your SmtpClient doesn't have the SMTP port open. Better check your host name.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: