Tuesday, December 16, 2008

System.Net.Mail.SmtpClient with login credentials (ASP.Net)

How to set login credentials for email ... C#...

Normally when I set up a SMTP form for a client using ASP.Net I do something like the following:

System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();
myMail.From = "
you@someemail.com";
myMail.Subject = "subject of email";
myMail.To.Add(new MailAddress("
them@someemail.com"));
myMail.Body="This is the body of the email...";

System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("myserver",25);
mailClient.Send(myMail);


I havent had to change that process until today. Now I needed to find ways to do "credentialed" email, since his web host required it. I didn't want to purchase/install any special active X object to do this.

SOLUTION:
However, there are some new tricks with ASP.net 2.0 to handle this. Its something I didnt have to use til today.

You can put some settings in the Web.config file to handle smtp when you do a .Send() call
http://aspnet.4guysfromrolla.com/articles/072606-1.aspx

Or you can do code-behind method to do this...
http://www.wwwcoder.com/main/parentid/435/site/5833/68/default.aspx

Both seem to work well, depending on your preferences.

OTHER BENEFITS:

I have noticed lately that some email servers (like hotmail) gives warning messages when I do un-credentialed emails... The email says "This message has been blocked for your safety." next to it... and "This message may be a phishing scam" or "This message may be dangerous". Not good. When use the email credentials I think it checks the IMX records - and sees what server the email should come from.

This is the link hotmail gives it.. about Microsoft's "Sender ID" checking. http://help.live.com/help.aspx?project=MailClassic&market=en-US&querytype=keyword&query=gnihsihp&tmt=&domain=mail.live.com&format=b1&fs=-1

No comments:

Post a Comment