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 %>
02: <%
03: 'Declare variables
04: Dim sMsg
05: Dim sTo
06: Dim sFrom
07: Dim sSubject
08: Dim sTextBody
09: Dim sHTMLBody
10:
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")
17:
18:'Only run this if it's not the first time
19: If Request.Form("Submit") <> "" Then
20:
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
30:
31:'Send the email
32: objMail.Send
33: 'Set sMsg which will be used later
34: sMsg = "Your message was sent to: " & sTo
35:
36: 'Clean-up
37: Set objMail = Nothing
38:
39: End If
40: %>
41: <html>
42: <head><title>SendMail</title></head>
43: <body>
44: <form action="<%=Request.ServerVariables("PATH_INFO")%>" method="post">
45: <table>
46: <tr>
47: <td>Send To:</td>
48: <td><input type="text" name="sTo" value="<%=sTo%>"></td>
49: </tr>
50: <tr>
51: <td>Send From:</td>
52: <td><input type="text" name="sFrom" value="<%=sFrom%>"></td>
53: </tr>
54: <tr>
55: <td>Message Subject:</td>
56: <td><input type="text" name="sSubject" value="<%=sSubject%>"></td>
57: </tr>
58: <tr>
59: <td>Message Text Body:</td>
60: <td><textarea cols="60" rows="5" name="sTextBody">
61: <%=sTextBody%></textarea></td>
62: </tr>
63: <tr>
64: <td>Message HTML Body:</td>
65: <td><textarea cols="60" rows="5" name="sHTMLBody">
66: <%=sHTMLBody%></textarea></td>
67: </tr>
68: <tr>
69: <td> </td>
70: <td><input type="submit" name="Submit" value="Submit"></td>
71: </tr>
72: </table>
73: <hr>
74: <%=sMsg%>
75: </form>
76: </body>
77: </html>
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.