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

Leave a Comment

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