Article
Send Email Using ASP on .Net Server or WinXP Pro
Page: 1 2
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