Send Email Using ASP on .Net Server or WinXP Pro

This article came together with the hope that it will help ASP developers make the transition from existing NT and Windows 2000 servers to Microsoft’s new Web server platforms, while supporting legacy ASP code. All code is commented and has been tested on Windows XP Pro SP1 and Windows Dot.Net Server 2003 SR1 and SR2.

Arguably one of the most useful tasks a Website can accomplish is to provide the ability to collect feedback from visitors and send that information to the responsible parties. Most developers who use ASP with NT or Windows 2000 have enjoyed the ease with which they can develop scripts that send and receive email. The dll that has provided the handy CDO mail object is called cdonts.dll. The long name is Collaboration Data Objects for Windows NT® Server or, as I like to call it, CDO for NT.

Microsoft and cdonts.dll

Microsoft, in its own way, also calls cdonts.dll CDO for NT.

Since the release of XP, I have been told a number of times that XP doesn’t run a certain mail script or the new .Net Server has the email locked down, etc… The reason XP and .Net Server spit out errors, rather than run ASP email scripts the way NT or Win2k used to, is because CDONTS is old technology.

When Windows 2000 was released, Microsoft indicated that the cdonts.dll was deprecated, and that the new mail object to use was cdosys.dll. Windows 2000 came with both CDO dll’s but the writing was on the wall. CDONTS was on its way out, and ASP developers should learn the new mail object, CDOSYS. Unfortunately, there hasn’t been a lot of information about how to port existing ASP scripts to work with CDOSYS.

Windows XP Pro and .Net Server 2003 no longer come with CDONTS, so if you run a CDONTS email script, you’ll get an error. Experienced ASP developers are probably thinking, “Well, I could always take a cdonts.dll from my Win2k distribution and register it on my XP or Dot.Net machine”. And you can, but that is in conflict with Microsoft’s End User License Agreement (EULA), and, as using the new mail object is relatively easy, why not take advantage of it? The new object requires less overhead and if you dig into it, you’ll find it has more functionality as well.

The Classic ASP Script

Here’s an example of a feedback email script using classic ASP/CDONTS.

<% 

'EMAIL FORM USING CLASSIC ASP AND CDONTS  
Dim varName, varEmail,  
varCompany, varTelephone, varComments, varSend, thisPage
varName  = Request.Form("txtName")
varEmail  = Request.Form("txtEmail")
varCompany  = Request.Form("txtCompany")
varTelephone  = Request.Form("txtPhone")
varComments  = Request.Form("txtComments")
varSend  = Request.Form("send")
thisPage  = Request.ServerVariables("SCRIPT_NAME")

If not varSend = 1 Then  
'OUTPUT THE CONTACT FORM

%>

<form action="<%= thisPage %>" method="post">
 <input type="hidden" name="send" value="1">  
   <p>Feel free to contact us using this web form</p>
   <p>&nbsp;</p>
     <table width="100%" border="0" cellspacing="0" cellpadding="0">
       <tr valign="top">  
         <td align="right">Name:</td>
         <td width="3%">&nbsp;</td>
         <td nowrap><input type="text" name="txtName" size="35"></td>
       </tr>
       <tr valign="top">  
         <td align="right">Company:</td>
         <td width="3%">&nbsp;</td>
         <td><input type="text" name="txtCompany" size="35"></td>
       </tr>
       <tr valign="top">  
         <td align="right">email:</td>
         <td width="3%">&nbsp;</td>
         <td nowrap><input type="text" name="txtEmail" size="35"></td>
       </tr>
       <tr valign="top">  
         <td align="right">Telephone:</td>
         <td width="3%">&nbsp;</td>
         <td><input type="text" name="txtPhone" size="25"></td>
       </tr>
       <tr valign="top">  
         <td align="right">Comments:</td>
         <td width="3%">&nbsp;</td>
         <td><textarea name="txtComments" rows="5"  
cols="35"></textarea></td>
       </tr>
       <tr valign="top">  
         <td align="right">&nbsp;</td>
         <td width="3%">&nbsp;</td>
         <td>&nbsp;</td>
       </tr>
       <tr valign="top">  
         <td align="right"><input type="submit"  
name="Submit" value="Submit"></td>
         <td width="3%">&nbsp;</td>
         <td><input type="reset" name="Reset"  
value="Reset" class="frm"></td>
       </tr>
     </table>
   </form>

<%

Else
 'SEND MAIL  
   
   Dim ObjMail
   Set ObjMail = Server.CreateObject("CDONTS.NewMail")
   objMail.From = varName & " <" & varEmail & ">"
   objMail.To = "your-name@your-website.com" ' Add your email address
   objMail.Subject = "Contact from Your Website"
   objMail.Body = varName & ""_  
       & VBCrLf & varCompany & ""_  
       & VBCrLf & varTelephone & ""_  
       & VBCrLf & VBCrLf & varComments & VBCrLf  
   objMail.Send

   Set objMail = Nothing

 'AND OUTPUT THANK YOU
   
 Response.Write "Hey "& varName & ",<br />"_  
     & VBCrLf & "Thanks for your feedback.<br />"_
     & VBCrLf & "We'll contact you as soon as possible!"  

End If
   
%>

This handy feedback page runs just fine on NT and Windows 2000 servers, but watch out! If you run the above script on a server without CDONTS support, you’ll be greeted with a nasty VB Script Error to indicate that you’ve called an invalid class of some sort. In order to make this script compliant with the new mail object CDOSYS, you’ll need to make a couple of minor changes.

Tweaks For CDOSYS Compliance

It’s really not all that difficult. The first block of ASP code and the main HTML form stay the same. Only the output block of ASP code needs to be changed.

As a bonus, we’ll use the HTMLBody attribute of the mail object to output our email as HTML. By default, this will also create a plain text version to support older email clients.

Make these changes to the last block of ASP code (note that changed code is bolded):

<%  
 
Else  
 
'SEND MAIL AND OUTPUT THANK YOU RESPONSE  
   
 'REPLACE LINE RETURNS WITH HTML BREAKS  
 varComments = Replace(varComments, chr(10), "<br />")  
 
 'THE MAIL OBJECT  
 Dim ObjMail  
 Set ObjMail = Server.CreateObject("CDO.Message")  
 objMail.From = varName & " <" & varEmail & ">"  
 objMail.To = "your-name@your-website.com" ' add your email address  
 objMail.Subject = "Contact from Your Website"  
 objMail.HTMLBody = varName & "<br />"_  
     & VBCrLf & varCompany & "<br />"_  
     & VBCrLf & varTelephone & "<br />"_  
     & CBCrLf & varComments & "<br />"
   
 objMail.Send  
 
 Set objMail = Nothing  
 
'AND OUTPUT THANK YOU  
     
Response.Write "Hey "& varName & ",<br />"_  
   & VBCrLf & "Thanks for your feedback.<br />"_  
   & VBCrLf & "We'll contact you as soon as possible!"  
 
End If  
 
%>

As you can see, only 3 lines of code needed to be adjusted to make the switch from CDONTS to CDOSYS, and the first change is to replace ascii line returns with the html line break tag. If we had used the plain text version objMail.TextBody, instead of objMail.HTMLBody, we would have had to make only 2 changes to the code.

During testing, however, I did run into a glitch…

A Glitch

On the betas of .Net Server SR1 and SR2, the above code should run without incident, but I had a heck of a time convincing it to run on WinXP Pro. I could not avoid these errors:

Error Type:  
CDO.Message.1 (0x80040220)  
The "SendUsing" configuration value is invalid.

The knowledgebase at msdn.Microsoft.com has some information about these errors for Exchange server, but the best references I found for ASP and Windows 2000/XP are included at the bottom of this article. I have since modified the original CDOSYS code to include some configuration scripting to prevent the “SendUsing” error. If you have problems running the code on your Windows XP Pro or Windows 2000 machine, make the following additions to your code:

Place this code at the top of the page before any other code appears (articles at www.asp101.com and www.markforum.tk inspired the following changes):

<!--METADATA TYPE="typelib"   
UUID="CD000000-8B95-11D1-82DB-00C04FB1625D"  
NAME="CDO for Windows 2000 Library" -->  
<!--METADATA TYPE="typelib"  
UUID="00000205-0000-0010-8000-00AA006D2EA4"  
NAME="ADODB Type Library" -->

The last block of code should look like this (note that changed code is bolded):

<%  
 
Else  
 
'SEND MAIL AND OUTPUT THANK YOU RESPONSE  
   
 'REPLACE LINE RETURNS WITH HTML BREAKS  
 varComments = Replace(varComments, chr(10), "<br />")  
 
 'THE MAIL OBJECT  
 Dim ObjMail  
 Set ObjMail = Server.CreateObject("CDO.Message")  
 
 Set objConfig = CreateObject("CDO.Configuration")  
 
 'Configuration:  
 objConfig.Fields(cdoSendUsingMethod) = cdoSendUsingPort  
 objConfig.Fields(cdoSMTPServer) = "localhost"  
 objConfig.Fields(cdoSMTPServerPort) = 25  
 objConfig.Fields(cdoSMTPAuthenticate) = cdoBasic    
 
 'Update configuration  
 objConfig.Fields.Update  
 Set objMail.Configuration = objConfig
 
 
 objMail.From = varName & " <" & varEmail & ">"  
 objMail.To = "your-name@your-website.com" 'Add your email address  
 objMail.Subject = "Contact from Your Website"  
 objMail.HTMLBody = varName & "<br />"_  
     & VBCrLf & varCompany & "<br />"_  
     & VBCrLf & varTelephone & "<br />"_  
     & CBCrLf & varComments & "<br />"    
 objMail.Send  
 
 Set objMail = Nothing  
 Set objConfig = Nothing  
 
'AND OUTPUT THANK YOU  
     
Response.Write "Hey "& varName & ",<br />"_  
   & VBCrLf & "Thanks for your feedback.<br />"_  
   & VBCrLf & "We'll contact you as soon as possible!"  
 
End If  
 
%>
Windows XP and Windows 2000 CDO Configuration Resources

The Microsoft Developer Network
http://msdn.microsoft.com

Sending Email Via an External SMTP Server Using CDO, at ASP101.com
http://www.asp101.com/articles/john/cdosmtprelay/default.asp

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments

Comments on this post are closed.