ASP CDO Email – Fixing the authentication error

As the developer here at High Speed Web, I get to deal with customers on special occasions. Generally these entail stand-offs between the technical support staff (which may or may not include network administrators) and the customers. The latter states that the problem is the server, while the former contends that it is the user’s code. So they bring in me, to either slap the administrators for not setting up their servers right or to show the user what we like to call the correct way to code. During my time here, I have helped several people out with their ASP scripts as pertaining to sending email. They all used CDO, and they all had the same problem: SMTP Authentication.

SMTP Authentication is fairly simple to do in ASP with CDO, but it isn’t obvious or apparent. I don’t quite understand Microsoft’s methodology here, but here it is:


Set msgTest = CreateObject("CDO.Message")
msgTest.Subject = "Testing smtp authentication"
msgTest.Sender = "testing@highspeedweb.net"
msgTest.From = "testing@highspeedweb.net"
msgTest.To = "you@yourdomain.edu"
msgTest.TextBody = "This is some sample message text.."

Pretty easy so far. Create a message object of type CDO.Message. Give it a subject, a sender, a from email address, a to email address, and some text as the body. Most everyone gets this part right. And then on…


'Tell it to use SMTP (1 is send using local SMTP pickup directory, 2 is SMTP server)
msgTest.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'Name or IP of Remote SMTP Server
msgTest.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.your.com"

'Use SSL for the connection (False or True)
msgTest.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
msgTest.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

Most people get these right as well, but what they are lacking is this:


'Type of authentication, 0 = NONE, 1 = Basic (Base64 encoded), 2 = NTLM
msgTest.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

'Your UserID on the SMTP server
msgTest.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "youruserid"

'Your password on the SMTP server
msgTest.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "yourpassword"

msgTest.Configuration.Fields.Update
msgTest.Send

Depending on your hosting provider, the user you use to send may be an email address. With our hosting customers it is. You will probably never use NTLM authentication, unless you do something on a corporate network with centralized authentication.

Well, that’s about it. It’s really straight forward, just some gotcha’s in the schemas and configuration fields.

7 thoughts on “ASP CDO Email – Fixing the authentication error
  1. Yes, it really works but:
    1 line: “Set msgTest = Server.CreateObject(“CDO.Message”)” should be Set msgTest = CreateObject(“CDO.Message”)

    and other line: “objMessage.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendpassword”) = “yourpassword” shoul be
    msgTest.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendpassword”) = “yourpassword”

  2. You are my God man… I was struggling with CDO for the past 3 days not understanding the importance of Authentication….and searching the web for a solution like a mad dog….no body was able to fix my problem….and you did it with your simple article…hats off to you….I am really thankful to you…

  3. Is there a way in using CDO to still send out the email even if one of several address is invalid or not found? Now get 8004020f error, want to just send to all valid address and not ‘choke’ because one is invalid. Thanks!

  4. Well, Mark… How is it formulated? You could loop through the array of email addresses, sending them individually with error catching and them ignore errors you come across. Other than that, there really isn’t a whole lot you can do since error 8004020f is that the message was rejected by the server, and since you can’t control the email server’s response (or at least you really don’t want to without creating major headaches), it’s best to do then individually and catch errors.

  5. Is there a way in using CDO to still send out the email even if one of several address is invalid or not found? Now get 8004020f error, want to just send to all valid address and not ‘choke’ because one is invalid. Thanks!

Leave a Reply