Often, it is necessary to have a page bundle some information and send it out by way of an e-mail message. As simple as that may sound, there are a number of different options that are available to accomplish that task. In addition, there are several pitfalls that need to be avoided.

Sending messages through Active Server Pages (ASP) should not be a difficult task. However, if you are having problems sending messages through ASP, an alternative exists to replace ASPMail. The alternative is called Collaboration Data Objects (CDO). While we have received numerous requests to use CDONTS (as opposed to CDO), this cannot be enabled as it requires a local IIS SMTP server. Currently, we cannot provide that service.

However, CDO v.2.0 is a very capable alternate for CDONTS. Built-in to Windows, CDO can be used by the customer to send mail. CDO, as is CDONTS, is configured to work with a local IIS SMTP server by default. However, unlike CDONTS it can be configured for use with a remote SMTP server.

CDO is both powerful and flexible enough to perform most of the major tasks that you may require of it. With CDO, you can send attachments, send to group lists, and send schedules (much as you can with, for instance, Microsoft® Outlook). CDO is easy-to-use and it allows considerable leeway in manipulating the information that gets delivered.

Getting Started

To creating an instance of a CDO object in your ASP code, it is as easy as:

<%
DIM objCDO
Set objCDO = Server.CreateObject("CDO.Message")
%>

Note: To improve the performance on -- and the stability of -- your web site, it is important that you remember to destroy every CDO object that you create. For instance, after you have finished with CDO: 

<% Set objCDO = Nothing %>

Now you're ready to send those e-mail messages! CDO.Message has a few obvious, easy-to-use properties and methods. That said, let's look at some code that makes the point clearly:

<%
myMail.From = "info@domain.com"
myMail.To = "jsmith@someisp.com"
myMail.Subject = "Information from domain.com"
myMail.Body = "Here's the information you requested"
myMail.Send
%>

CDO has some extraneous features that can be added. Other handlers (methods) such as Cc, Bcc, AttachFile and Importance can be implemented. The Importance property takes three values: 0 (Low), 1 (Normal), 2 (High); its default is 1, Normal.

As mentioned above, some configuration is required to use CDO for Windows (CDOSYS) and a remote SMTP server. A Configuration object is created, populated with configuration information, and then associated with a Message object using the objCDO.Configuration property. The SMTP server name, SMTP connection timeout, and cdoSendUsingPort are the only credentials that are needed.

To create the Configuration object, you would use the following:

<%
Dim objConf
Set objConf = Server.CreateObject("CDO.Configuration")
%>

The following type libraries must be specified at the top of the page:

<!--METADATA TYPE="typelib" UUID="CD000000-8B95-11D1-82DB-00C04FB1625D" NAME="CDO for Windows Library" -->

<!--METADATA TYPE="typelib" UUID="00000205-0000-0010-8000-00AA006D2EA4" NAME="ADODB Type Library" -->

Note: You can also use the following type library. However, should the file path ever change, we recommend using the first two.

<!--METADATA TYPE="TypeLib" FILE="C:\WINNT\system32\cdosys.dll" -->

Then, you can automatically use the CDO constants in your page.

<%
Dim Flds
Const cdoSendUsingPort = 2
Set Flds = iConf.Fields
With Flds
  .Item(cdoSendUsingMethod) = cdoSendUsingPort
  .Item(cdoSMTPServer) = "mail-fwd"
  .Item(cdoSMTPServerPort) = 25
  .Item(cdoSMTPconnectiontimeout) = 10
  .Update
End With
%>

Example: CDO (E-Mail Script) in its entirety

<!--METADATA TYPE="typelib" UUID="CD000000-8B95-11D1-82DB-00C04FB1625D" NAME="CDO for Windows Library" -->
<!--METADATA TYPE="typelib" UUID="00000205-0000-0010-8000-00AA006D2EA4" NAME="ADODB Type Library" -->
<%
SUB sendmail( fromWho, toWho, Subject, Body )
Dim objCDO
Dim iConf
Dim Flds

Const cdoSendUsingPort = 2

Set objCDO = Server.CreateObject("CDO.Message")
Set iConf = Server.CreateObject("CDO.Configuration")
 
Set Flds = iConf.Fields
With Flds
           .Item(cdoSendUsingMethod) = cdoSendUsingPort
           .Item(cdoSMTPServer) = "mail-fwd"          
           .Item(cdoSMTPServerPort) = 25
           .Item(cdoSMTPconnectiontimeout) = 10
           .Update
End With

Set objCDO.Configuration = iConf

objCDO.From = fromWho
objCDO.To = toWho
objCDO.Subject = Subject
objCDO.TextBody = Body
objCDO.Send

END SUB

fromWho = TRIM( Request.Form( "fromWho") )
toWho = TRIM( Request.Form( "toWho") )
Subject = TRIM( Request.Form( "Subject" ) )
Body = TRIM( Request.Form( "Body") )
IF toWho <> "" THEN
sendMail fromWho, toWho, Subject, Body

'Cleanup
Set ObjCDO = Nothing
Set iConf = Nothing
Set Flds = Nothing
' Any existing page can be used for the response redirect method
Response.redirect "confirmation.html"
END IF 
%>

<HTML>
<HEAD><TITLE>Email Form</TITLE></HEAD>
<FORM METHOD="POST" ACTION="<%=Request.ServerVariables("SCRIPT_NAME")%>">
<BR>TO: <INPUT NAME="toWho" TYPE="text" SIZE=40>
<BR>FROM: <INPUT NAME="fromWho" TYPE="text" SIZE=40>
<BR>SUBJECT: <INPUT NAME="Subject" TYPE="text" SIZE=40>
<BR><TEXTAREA NAME="Body" COLS=40 ROWS=5></TEXTAREA>
<BR><INPUT TYPE="SUBMIT" VALUE="Send Mail">
</FORM>
</HTML>

Note that our Technical Support Department does not assist with scripting. The script given here is a test script that you may want to incorporate into your code. You would need to make sure that any paths referenced in the code are correct.

Recommended CDO-related Links

CDO for Windows

http://msdn.microsoft.com/library/en-us/cdosys/html/f928be29-59e2-4341-86c9-31842075775a.asp

CDO for Windows (Configuration Interface)

http://msdn.microsoft.com/library/en-us/cdosys/html/783b88a2-7755-4908-90bc-8aeb0bdf95a5.asp

Adding Attachments (CDO)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/c1e10b9b-f9d2-4041-b1cc-4c9c6e85ae19.asp

Please note: the information on this page applies to ITS web hosting plans. It may or may not apply to other environments. If you are looking for a feature described here, or better support from your hosting provider, please consider hosting your site with ITS!

1555 N Naperville/Wheaton Road, Suite 107
Naperville, IL 60563
phone 630.420.2550
fax 630.420.2771