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 ...

23 comments:

the said...

Top website designing company in India, Surat provding world class design and solutions.Surat SEO, SEO Company Surat, Website Design Services Surat, Surat Website Development Services

Web Design Company said...

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

Alex said...

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.

virtual assistant services said...

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

Melbourne embroidery said...

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

Botox Chicago said...

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

Web Hosting India said...

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

Fort Lauderdale criminal lawyer said...

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

Dropshipper said...

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.

online auction said...

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!

S3 Technology Softwares said...

thanks dear

Ultimate appliances said...

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

Argan Oil said...

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

apartments in gainesville said...

This is a good common sense Blog. Very helpful to one who is just finding the resources about this part. It will certainly help educate me.

apartments in gainesville

Vancouver Internet Marketing said...

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

Vancouver Chiropractor said...

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

Channing Tatum workout said...

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

freelance web designer said...

really I need this information thanks for needful information

SEO Delhi said...

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

Unknown said...

very cool tag and backlink hear and buy
loose diamonds

rocky case said...

Stephen Stapinski

Really your blog is very interesting.... it contains great and unique information. I enjoyed to visiting your blog. It's just amazing.... Thanks very much for the share.

Rajinder Singh said...

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

Web Designer India

Web Design Company India said...

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