Since one of the attendees at my PDC pre-con asked about this, I thought I'd pass it along. I've updated the sample code from my ASP.NET Overview pre-con session to add the necessary code for performing authentication when sending mail using an SMTP server that requires authentication (and requiring authentication for your SMTP server is a Good Thing®).

System.Web.Mail is just a wrapper over CDOSYS, and the v1.1 framework provides a new Fields member (on the MailMessage class) that provides access to any fields available in CDOSYS, by specifying the namespace for that field (more info on the fields available can be found here). So the code for authenticating a mail request is:

  'Use the following fields to add authentication, if necessary
  '0 = None / 1 = Basic / 2 = NTLM
  Mail.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
  Mail.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myUser"
  Mail.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "myPwd"

Do keep in mind that when using basic authentication, credentials are passed in clear text, so it is especially important to make use of SSL encryption if you are using basic authentication.

Thanks to Darren Jefford, from Microsoft UK, for working this out.

Filed under:

Comments

# re: PDC Code Update

Sunday, November 02, 2003 4:32 AM by Colt

Hi Andrew,

Thanks for sharing. Mike pin a very useful thread on the Forums about SMTP Mailing as well.
FYI: http://weblogs.asp.net/coltk/posts/31875.aspx

# re: PDC Code Update

Sunday, November 02, 2003 5:16 AM by Justin

Your my hero! I've been looking for this for a very long time...

# Authentication and System.Web.Mail

Sunday, November 02, 2003 4:57 PM by TrackBack

# SMTP authentication through System.Web.Mail

Friday, December 12, 2003 9:29 AM by TrackBack

# Doing Authentication in System.Mail

Wednesday, May 05, 2004 11:47 PM by TrackBack

# Blogging for the sake of Blogging

Wednesday, June 02, 2004 3:54 PM by TrackBack

# re: PDC Code Update

Saturday, June 05, 2004 12:00 AM by Babak (From Iran)

The code is very useful and it works magically

# smtp authentication

Friday, September 11, 2009 8:01 AM by Cengiz Han

smtp authentication

# smtp authentication

Sunday, September 13, 2009 10:37 AM by Cengiz Han

smtp authentication

# Doing Authentication in System.Mail

Friday, October 30, 2009 5:55 AM by Jeff's Blog

Doing Authentication in System.Mail

# re: PDC Code Update

Monday, March 22, 2010 2:31 PM by Macaria

Greeting. In the right light, at the right time, everything is extraordinary. Help me! Need information about: India share market trading call tomorrow. I found only this - <a href="tt.tlu.ee/.../ShareTrading">share trading signals nashik</a>. Share trading, during the finite circulation of the same managers, there was a different trading in the collaboration and transaction of big risk commentators, which found to make up their 'short to the trading. Although market of 50 definition of months gives introduction in 50 change time of a firm, it becomes once obtain the effect the excercise to harmonize a processing's matter, exchange, stocks, or annual counter, share trading. Thanks :mad:. Macaria from Marshall.

Leave a Comment

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