Article
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> </p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr valign="top">
<td align="right">Name:</td>
<td width="3%"> </td>
<td nowrap><input type="text" name="txtName" size="35"></td>
</tr>
<tr valign="top">
<td align="right">Company:</td>
<td width="3%"> </td>
<td><input type="text" name="txtCompany" size="35"></td>
</tr>
<tr valign="top">
<td align="right">email:</td>
<td width="3%"> </td>
<td nowrap><input type="text" name="txtEmail" size="35"></td>
</tr>
<tr valign="top">
<td align="right">Telephone:</td>
<td width="3%"> </td>
<td><input type="text" name="txtPhone" size="25"></td>
</tr>
<tr valign="top">
<td align="right">Comments:</td>
<td width="3%"> </td>
<td><textarea name="txtComments" rows="5"
cols="35"></textarea></td>
</tr>
<tr valign="top">
<td align="right"> </td>
<td width="3%"> </td>
<td> </td>
</tr>
<tr valign="top">
<td align="right"><input type="submit"
name="Submit" value="Submit"></td>
<td width="3%"> </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.
Andrew is a partner at