ASP.NET Tip: Sending Mail with ASP.NET 2.0

In ASP.NET 2.0, Microsoft deprecated the System.Web.Mail namespace and replaced it with System.Net.Mail. The new library introduces some new features, but it also includes some bugs in how mail is sent. Before discussing some of these in detail, let's go through some code sample (which assumes you've added a using System.Net.Mail at the top of the file):

MailMessage msg = new MailMessage();
msg.From = new MailAddress("address@domain.com", "Person's Name");
msg.To.Add(new MailAddress("destination@domain.com",
                           "Addressee's Name");
msg.To.Add(new MailAddress("destination2@domain.com",
                           "Addressee 2's Name");
msg.Subject    = "Message Subject";
msg.Body       = "Mail body content";
msg.IsBodyHtml = true;
msg.Priority   = MailPriority.High;
SmtpClient c   = new SmtpClient("mailserver.domain.com");
c.Send(msg);

The code is similar with some minor changes to how you address the message. Instead of constructing an address, you can let the system do that for you. If you specify an e-mail address and a name, it will automatically display in the message as this:

"Person's Name" <destination@domain.com>

This is the "proper" form for an e-mail address. You can add multiple addresses to the To, CC, and BCC collections in the same way as shown above. This is programmatically easier to do than sending lots of messages—just add multiple addresses to the BCC property in order to send a mass mailing.

Now, About Those Bugs...

As previously mentioned, this new namespace has a couple of bugs. The first is when you send a message the headers are all added in lowercase letters. While the RFC for SMTP mail doesn't specify how the headers should be capitalized, many spam filters restrict messages where the headers are not properly capitalized.

The other bug deals with the Priority setting, which should mark a message as important within the mail client. Because of the way the header is formatted, my mail program (Eudora) doesn't recognize it as the priority flag and doesn't mark the message as important. While this seems trivial, it's a change from the System.Web.Mail version for no apparent reason. I'm continuing to research this and if I can't find a good fix, I may switch back to System.Web.Mail and deal with the warning messages that Visual Studio displays about System.Web.Mail being deprecated.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.



Comments

  • load images from database

    Posted by johnpie99 on 03/16/2007 08:39pm

    ?  load images from database

    Reply
  • It's even worse...

    Posted by Markus Wolters on 02/08/2007 12:54pm

    It's even worse with System.Net.Mail. I am going to implement my own classes. It's all about "Content-transfer-encoding". The implementation of "quoted-printable" is broken. First it's possible to create lines above 76 characters in length. That's against the RFC and it's also a criteria for some SPAM blockers to recognize those mails as SPAM.) And the class also converts CRLF's to =0D=0A. It's not even against the RFC again, it's also very dirty to look at if you don't have a MIME-Client. But the greatest is this: if you're going to use TransferEncoding.SevenBit it's doing what it's supposing: "content-transfer-encoding: sevenbit". Again, it's not RFC standard, and clients like AOL are getting totally out of line. So for me, it's useless! And you even cannot get to fix it because all of those classes are sealed or internal or whatever! It just sucks...

    Reply
  • No bug like those

    Posted by jasonli on 10/27/2006 03:26pm

    I use System.Net.Mail to send email from my web application. Everything is fine, no bug got like mentioned above. Mail Server is MDaemon.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Email is the most common communication vehicle used by organizations of all shapes and sizes. Among the billions of email messages sent every day are sensitive information, critical requests, and other essential business data. IT staff bear the burden of ensuring the confidentiality, integrity, and availability of the information contained within the communication. This white paper explores the email security landscape, an assessment of the threats organizations face,  and the building blocks of an effective …

  • Between contractors and employees, there is a great deal of risk surrounding the privacy and security of sensitive data in the healthcare industry. When a breach is suspected, the law requires that healthcare organizations quickly launch an investigation to find the source and scope of the issue. These investigations can be quite costly, to say nothing of the penalties for the breach itself and the damage to reputation and brand. This white paper details new affordable software tools that can help healthcare …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds