Before starting, I should mention that if you have never used CDONTS or CDOSYS and just want to find out how to create a feedback form, read on, this is also written for you. This article is targeted at Classic ASP developers. If you are using ASP.NET, you may want to check out an article by Brad Kingsley on Sending Email from ASP.NET.
Now back to the topic at hand. If you currently use CDONTS for sending email, the time may have come to bury the dead and move on. Microsoft introduced CDONTS with IIS4, allowing programmers to easily send email from ASP without purchasing and installing 3rd-party components. But, like any evolving technology, a new version has taken center stage. CDOSYS is Microsoft's new and improved interface for SMTP email which was introduced in Windows 2000.
So, the question: "Why should I switch to CDOSYS when my current CDONTS code works great?". Let me give a few reasons:
- CDONTS has been deprecated in IIS5 and is completely removed from Windows Server 2003, and even Windows XP. Yes, it is possible to install on Windows Server 2003 but with the performance improvements and other enhancements, there should be no need to do so.
- CDOSYS carries less overhead than CDONTS.
- HTML functionality is much improved with automatic generation of the TextBody by just setting the HTMLBody property.
- There are some new properties and methods that should have been included with CDONTS but weren't.
Let's look at an example.
01: <% Option Explicit %>
03: 'Declare variables
04: Dim sMsg
05: Dim sTo
06: Dim sFrom
07: Dim sSubject
08: Dim sTextBody
09: Dim sHTMLBody
11: 'Get data from previous page
12: sTo = Request("sTo")
13: sFrom = Request("sFrom")
14: sSubject = Request("sSubject")
15: sTextBody = Request("sTextBody")
16: sHTMLBody = Request("sHTMLBody")
18:'Only run this if it's not the first time
19: If Request.Form("Submit") <> "" Then
21: Dim objMail
22: 'Create the mail object
23: Set objMail = Server.CreateObject("CDO.Message")
24: 'Set key properties
25: objMail.From = sFrom
26: objMail.To = sTo
27: objMail.Subject= sSubject
28: objMail.TextBody = sTextBody
29: objMail.HTMLBody = sHTMLBody
31:'Send the email
33: 'Set sMsg which will be used later
34: sMsg = "Your message was sent to: " & sTo
37: Set objMail = Nothing
39: End If
44: <form action="<%=Request.ServerVariables("PATH_INFO")%>" method="post">
47: <td>Send To:</td>
48: <td><input type="text" name="sTo" value="<%=sTo%>"></td>
51: <td>Send From:</td>
52: <td><input type="text" name="sFrom" value="<%=sFrom%>"></td>
55: <td>Message Subject:</td>
56: <td><input type="text" name="sSubject" value="<%=sSubject%>"></td>
59: <td>Message Text Body:</td>
60: <td><textarea cols="60" rows="5" name="sTextBody">
64: <td>Message HTML Body:</td>
65: <td><textarea cols="60" rows="5" name="sHTMLBody">
69: <td> </td>
70: <td><input type="submit" name="Submit" value="Submit"></td>
The above code in an ASP page uses CDOSYS and will allow the user to specify the To, From, Subject and both the Text and HTML bodies. Just cut and past into a blank page, remove the line numbers and save with an extension of .asp and this should work for you. In a real world example, you probably don't need both body options but this lets you see how easy you can set each part. In fact, if you just add to the HTMLBody property without adding the TextBody property, the TextBody will be generated automatically for you for backward compatibility with older email clients. Nice! Notice that the Body Property has been replaced with TextBody and HTMLBody and that the BodyFormat doesn't exist.
Even with the added features of CDOSYS, you'll find it is as easy as ever to work with. For those that have been loyal to third-party components, CDOSYS may do everything you need. Not only that, if you're using Windows 2000 or Windows XP, it's available to you as we speak. All the best!
For more Information:
Visit Microsoft's documentation directly: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_imessage_interface.asp
By Scott Forsyth, a senior web specialist at ORCS Web, Inc. - a company that provides managed hosting services for clients who develop and deploy their applications on Microsoft Windows platforms.