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. Thus:
<% Set objCDO = Nothing %>
Now you're ready to send those e-mails 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")
%>
Here is a list of the configuration fields that
are to be used:
<%
Const cdoSendUsingPort = 2
Dim objFields
Set objFields = objConf.Fields
objFields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")
= cdoSendUsingPort objFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")
= "mail-fwd" objFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout")
= 10
objFields.Update
%>
Below is another way to specify the configuration fields. This method doesnt
use the schema paths. This is a simpler way of calling the aforementioned paths.
You can import the type library 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 1: CDO (E-Mail Script) in its entirety (using
schema path)
<%
SUB sendmail( fromWho, toWho, Subject, Body )
Dim objCDO
Dim objConf
Dim objFields
Const cdoSendUsingPort = 2
Set objCDO = Server.CreateObject("CDO.Message")
Set objConf = Server.CreateObject("CDO.Configuration")
Set objFields = objConf.Fields
objFields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")
= cdoSendUsingPort objFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")
= "mail-fwd" objFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout")
= 10
objFields.Update
Set objCDO.Configuration = objConf
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
Response.redirect "confirmation.html"
' Any existing page can be used for the response redirect method
END IF
'Cleanup
Set ObjCDO = Nothing
Set objConf = Nothing
Set objFields = Nothing
%>
<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>
Example 2: CDO (E-Mail Script) in its entirety (using CDO & ADODB type
libraries)
<!--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
Response.redirect "confirmation.html"
END IF
'Cleanup
Set ObjCDO = Nothing
Set iConf = Nothing
Set Flds = Nothing
%>
<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 and CDONT-related Links
CDO for Windows
http://msdn.microsoft.com/library/en-us/cdosys/html/_cdosys_about_cdo_for_windows_2000.asp?frame=true
CDONTS (1.2)
http://msdn.microsoft.com/library/en-us/cdo/html/_olemsg_overview_of_cdo.asp?frame=true
CDO for Windows (IConfiguration Interface)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_iconfiguration_interface.asp