Pages

Advertisement

Saturday, July 14, 2007

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.

 

Hey it was really simple in asp.net as compared to any other languages ...

19 comments:

  1. Only some very good tools have a head for resolving this problem. To my wat of thinking one of them is the next. Reason of the software relieved me in similar trouble couple days ago and owns many useful capabilities - recover outlook 2003 pst.

    ReplyDelete
  2. Interesting and important information. It is really beneficial for us. Thanks

    ReplyDelete
  3. Wow, Fantastic article, it’s so helpful to me, and your blog is very good,

    ReplyDelete
  4. Wow, Fantastic article, it’s so helpful to me, and your blog is very good,

    ReplyDelete
  5. Hi friend...This was amazing article.Interesting and much more informative content.Thanks for sharing..

    ReplyDelete
  6. Interesting layout on your blog. I really enjoyed reading it and also I will be back to read more in the future.

    ReplyDelete
  7. Thanks for this post. It Very nice Blog. It was a very good Blog. I like it. Thanks for sharing knowledge. Ask you to share good Blog again.

    ReplyDelete
  8. Thanks for ones marvelous posting! I seriously enjoyed reading it, you're a great author.I will be sure to bookmark your blog and definitely will come back in the foreseeable future. I want to encourage you continue your great job, have a nice day!

    ReplyDelete
  9. Interesting layout on your blog. I really enjoyed reading it and also I will be back to read more in the future.

    ReplyDelete
  10. Interesting and important information. It is really beneficial for us. Thanks

    ReplyDelete
  11. Thank you, thats very interesting information. I need to share with my friends.

    ReplyDelete
  12. Another informative blog… Thank you for sharing it… Best of luck for further endeavor too.

    ReplyDelete
  13. I’ve learned a lot from your blog here, Keep on going, my friend, I will keep an eye on it.

    ReplyDelete
  14. really I need this information thanks for needful information

    ReplyDelete
  15. I got many information and some important tips with the help of this post. I would like to visit again.

    ReplyDelete
  16. very cool tag and backlink hear and buy
    loose diamonds

    ReplyDelete
  17. Interesting blog. I really enjoy for read this blog.......

    Web Designer India

    ReplyDelete
  18. Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck! Web Design Company India

    ReplyDelete