Migrating from System.Web.Mail to System.Net.Mail

I discovered how great the new System.Net.Mail namespace is this weekend.

The now obsoleted System.Web.Mail was just a wrapper around CDONTS. It worked pretty well. It would churn out messages into the C:\InetPub\MailRoot\Pickup folder. So in a web farm we would configure the smtp service to route mail to a "smarthost". In our case we configured the smart host to route the mail through our exchange server. You could write your own code to set the relay server most likely reading the setting from your web.config’s appSettings.

After upgrading a web project to ASP.NET 2.0 I noticed several warnings about System.Web.Mail being obsolete and I should use System.Net.Mail. Well, this turned out to be a brain dead simple thing to convert and in the end it works even better. Here is the old way:

MailMessage message = new MailMessage();

message.To = "to@foo.com";

message.From = "from@bar.com";

message.Subject = "Hello subject";

message.Body = "Hello body";

 

// you can set the relay server in code like this:

// SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["SmtpServer"];

SmtpMail.Send(message);

All you have to do to convert is change your using statement from System.Web.Mail to System.Net.Mail and use and instance of SmtpClient class rather than the static SmtpMail class:

 

MailMessage message = new MailMessage();

message.To = "to@foo.com";

message.From = "from@bar.com";

message.Subject = "Hello subject";

message.Body = "Hello body";

 

SmtpClient smtp = new SmtpClient();

smtp.Send(message);

 

Not a lot of difference there. But here’s the cool thing. You can control how it sends the email with the web/app.config like this:

 

  <system.net>

    <mailSettings>

      <smtp deliveryMethod="Network">

        <network host="mail.mydomain.com" port="25" />

      </smtp>

      <!-- Use this setting for development

      <smtp deliveryMethod="SpecifiedPickupDirectory">

        <specifiedPickupDirectory pickupDirectoryLocation="C:\Temp" />

      </smtp>

      -->

    </mailSettings>

  </system.net>

 

Now I can have it drop the messages in a temp folder when I’m developing and have the production web.config relay the email through the main email server.

 

It’s also worth noting that the old System.Web.Mail.SmtpMail was single threaded. If you needed to do a lot of email processing it would totally suck. Take a look at what the send on the old one did:

 

public static void Send(MailMessage message)
{
      lock (SmtpMail._lockObject)
      {
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                  throw new PlatformNotSupportedException(SR.GetString("RequiresNT"));
            }
            if (Environment.OSVersion.Version.Major <= 4)
            {
                  SmtpMail.CdoNtsHelper.Send(message);
            }
            else
            {
                  SmtpMail.CdoSysHelper.Send(message);
            }
      }
} 

Yuck! This was a nice bottleneck. As apposed to:

public class SmtpClient
{
      // Events
      public event SendCompletedEventHandler SendCompleted;
 
      // Methods
      public void Send(MailMessage message);
      public void Send(string from, string recipients, string subject, string body);
      public void SendAsync(MailMessage message, object userToken);
      public void SendAsync(string from, string recipients, string subject, string body, object userToken);
      public void SendAsyncCancel();
}

SmtpClient is not a static class like SmtpMail was. So you can have many email sends going at the same time. Plus notice the Async versions of the Send method. This is much more conducive to NT services or Windows Forms applications that need to send emails.

 

Published Sunday, April 23, 2006 12:08 PM by findleyd
Filed under: , ,

Comments

# Thanks

Sunday, April 23, 2006 2:09 PM by Catalin Radoi
Thanks! This is a kick ass article! Now I can build my long-desired newsletter service.

# re: Migrating from System.Web.Mail to System.Net.Mail

Monday, April 24, 2006 11:12 AM by Jason R
It is my understanding that the MailMessage object To: and From: have changed...

The To: is now a collection of MailAdresses (so you'd add it as message.To.Add("email@email.com")

The From: is a single MailAddress..

Not sure how you got your code to compile, unless you're still using the old MailMessage?

# re: Migrating from System.Web.Mail to System.Net.Mail

Tuesday, May 29, 2007 4:58 PM by Ernesto Chaves

Hi. Sorry, but that code isn't working. As Jason R said, it's impossible 'cause to and from are now different. The first thing is that the "to" property is now read only.

# re: Migrating from System.Web.Mail to System.Net.Mail

Tuesday, May 29, 2007 5:05 PM by Ernesto Chaves

oMessage.To.Add(New MailAddress(sTo)) is a better way to access the To property.

# re: Migrating from System.Web.Mail to System.Net.Mail

Monday, October 29, 2007 11:45 AM by Tom Amsden

Hi David,

I would like to tap into your experience with a unique problem.

I have a client that hosts their website with a 3rd party that does not support 'code behind'.  I've created an .asp 'Address Change' form that I want to email to the client's email address when filled in correctly.  My client hosts their own e-mail with MS Exchange.

I use System.Web.Mail and pass an HTML forms page to the body.

I tests fine locally with the 3rd party host's default SMTP Server as the address of the System.web.mail SMTP server (I guess it defaults to the client's network Exchange SMTP).

It does not work when implemented on the Web.  I cannot connect to the client's Exchange SMTP even with the proper CDO authentification.

Does System.net.mail do this better?  What does the web host have to have installed to run System.Net.mail?

Any info would be appreciated.

Tom  

# re: Migrating from System.Web.Mail to System.Net.Mail

Monday, October 29, 2007 11:48 AM by Tom Amsden

Hi David,

I just sent a message regarding a problem with System.web.mail on a 3rd party web host.

I neglected to include a return e-mail.

Thanks Tom

# Testing Email Messages Sent using System.Net.Mail on Windows Vista

Thursday, February 21, 2008 4:56 PM by Dan Wahlin's WebLog

I've been developing for months on Windows Vista and had everything I needed at my disposal.&#160; Last

Leave a Comment

(required) 
(required) 
(optional)
(required)