File
summary
Once
your system is properly configured, we're going to use two sets
of pages that will demonstrate how to use the CDONTS object.
These pages will demonstrate how to send a message, attach documents,
control formatting and encoding properties, enable HTML within
message body, and add custom message headers. In this article,
we'll use files with the .htm extensions as the data input pages,
and pages with the .asp extensions to process the CDONTS objects.
Table A illustrates the page names and descriptions in this
month's Zip file.
Table
A: File Listing
| Page
name |
Description |
| cdonts_simpleInput.htm |
Basic CDO
for NTS Input page |
| cdonts_simpleInput.asp |
Basic CDO
for NTS processing page |
Using
the CDONTS NewMail object
Let's
get started by discussing the CDO for NTS object model. The
CDO for NTS Object Model consists of multiple objects. The NewMail
object allows you to create and send new mail messages. For
the purposes of this article, we're only concerned with creating
new messages, and we're only going to focus on using the NewMail
object. If you've ever used an Internet mail program, you'll
notice similar properties when creating a new mail message as
in the NewMail object. The NewMail property descriptions are
shown in Table B.
Table
B: CDONTS properties and descriptions
| Property |
Description |
| Bcc |
Sends
a blind copy of the message to this semicolon-separated
list |
| Body |
Contains
the content of your message in text of HTML format |
| BodyFormat |
Sets
the text format of the message |
| Cc |
Sends
a copy of the message to the semicolon-separated list |
| ContentBase |
Sets
the base property for URLs in the body of the message |
| ContentLocation |
Sets
the absolute or relative paths for URLs contained in
the message's body |
| From |
Sets
the sender's address |
| Importance |
Sets
the level of importance of the sent message |
| MailFormat |
Sets
the mail encoding format to MIME or text format |
| Subject |
Sets
the subject of the message |
| To |
Sends
the message to this semicolon-separated list |
| Value |
Sets
additional header information for the message |
| Version |
Retrieves
the version of the CDONTS.DLL |
After
setting the NewMail properties, you can use the NewMail methods,
shown in Table C, to perform actions on a message. These actions
include adding attachments and URLs, sending messages, and setting
local IDs.
Table
C: CDONTS method descriptions
| Method |
Description |
| AttachFile |
Adds
an attachment to the current message |
| AttachURL |
Adds
a URL as an attachment |
| Send |
Sends
the message to the TO, CC, BCC recipient list |
| SetLocaleIDs |
Sets
the user's local messaging environment variables |
Using
basic CDONTS techniques
Consider
the following situation: we want to automatically send a message
to notify the network administrator that a specific amount of
concurrent HTTP connections was reached. Granted, in most situations
when you're building messaging systems that allow applications
to alert or notify external processes, you won't build a user
input page. However, for the sake of this demonstration, we've
created a user input screen, named cdonts.htm. This Web
page, shown in Figure C, enables the user to specify whom to
send the message to, from whom the message is sent, the subject
matter, and set the Importance level and enter the message text.
Figure
C: cdonts.htm as viewed in the browser. Click here to see
a working CDONTS page: http://support.nshosts.com/cdonts.htm
Now,
let's take a closer look at the individual pages that make this
email processing possible. First, let's review the HTML code
that's used to collect the data cdonts.htm. Upon review, you'll
notice that this page contains HTML Form elements with FrontPage
validation and posts its results to cdonts.asp for processing,
as seen in Listing A.
Listing
A: cdonts.htm
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Language" CONTENT="en-us">
<META NAME="GENERATOR" content="Microsoft FrontPage 4.0">
<META NAME="ProgId" content="FrontPage.Editor.Document">
<TITLE>CDO for NTS -Simple Input Page</TITLE>
</HEAD>
<BODY>
<FORM METHOD="post" ACTION="cdonts.asp"
ONSUBMIT="return FrontPage_Form1_Validator(this)"
NAME="FrontPage_Form1">
<TABLE BORDER="0" width="100%">
<TR>
<TD VALIGN="top" ALIGN="left">From:</TD>
<TD VALIGN="top" ALIGN="left">
<P><!--WEBBOT BOT="Validation" STARTSPAN
S-DISPLAY-NAME="From"
S-DATA-TYPE="String" B-ALLOW-LETTERS="TRUE"
B-ALLOW-DIGITS="TRUE"
B-ALLOW-WHITESPACE="TRUE" S-ALLOW-OTHER-CHArs="@."
B-VALUE-REQUIRED="TRUE" I-MINIMUM-LENGTH="4" --><!--WEBBOT
BOT="Validation" endspan -->
<INPUT NAME="txtFrom" SIZE="45"></P>
</TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left">To:</TD>
<TD VALIGN="top" ALIGN="left"><!-- WEBBOT BOT="Validation"
STARTSPAN
S-DISPLAY-NAME="To" S-DATA-TYPE="String"
B-ALLOW-LETTERS="TRUE"
B-ALLOW-DIGITS="TRUE" S-ALLOW-OTHER-CHARS="@."
B-VALUE-REQUIRED="TRUE"
I-MINIMUM-LENGTH="7" --><!-- WEBBOT BOT="Validation"
ENDSPAN --><INPUT NAME="txtTo" SIZE="45">
</TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left">Subject:</TD>
<TD VALIGN="top" ALIGN="left">
<INPUT NAME="txtSubject" SIZE="45"></TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left">Importance:</TD>
<TD VALIGN="top" ALIGN="left">
<INPUT TYPE="radio"
NAME="optImportance" VALUE="2">High
<INPUT TYPE="radio"
CHECKED NAME="optImportance" VALUE="1">
Normal
<INPUT TYPE="radio" NAME="optImportance" VALUE="0">Low</TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left">Message:</TD>
<TD VALIGN="top" ALIGN="left"><TEXTAREA COLS=68
NAME=TXTMESSAGE ROWS=9>Type your message here in text
format</TEXTAREA></TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left" COLSPAN="2">
<P ALIGN="center"><INPUT TYPE="submit"
VALUE="Send Message" NAME="btnSend"
TABINDEX="1">
<INPUT TYPE="reset" VALUE=" CLEAR " NAME="btnClear"
TABINDEX="2"></P></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
The page
cdonts.asp accepts the posted data and process the CDONTS messaging
request. In addition, this page displays results back to the
user.
Listing
B: cdonts.asp
<%@ LANGUAGE="VBSCRIPT" %>
<%
Option Explicit
On Error Resume Next
Sub WriteHTML(strInput)
Response.Write(Server.HTMLEncode(strInput) & "<BR>")
End Sub
%>
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="Microsoft FrontPage 4.0">
<TITLE>Send CDONTS - Simple Input</TITLE>
</HEAD>
<BODY>
<%
Dim objMsg, strFrom, strTo, strSubject, strBody,
lngImportance
strFrom = Trim(Request.Form("txtFrom"))
strTo = Trim(Request.Form("txtTo"))
strSubject = Trim(Request.Form("txtSubject"))
strBody = Trim(Request.Form("txtMessage"))
lngImportance = Trim(Request("optImportance"))
Set objMsg = Server.CreateObject("CDONTS.NewMail")
objMsg.From = strFrom
objMsg.To = strTo
objMsg.Subject = strSubject
objMsg.Body = strBody
objMsg.Importance = lngImportance
objMsg.Send
'=== Alternatively you could have sent this in one line:
'objMsg.Send strFrom, strTo, strSubject, strBody,
lngImportance
Set objMsg = Nothing
WriteHTML("The following message was sent via CDO for NTS:")
WriteHTML("From: " &strFrom)
WriteHTML("To: " &strTo)
WriteHTML("Subject: " &strSubject)
WriteHTML("Importance: "&lngImportance)
WriteHTML("Body: " &strBody)
%>
<HR>
<A HREF = "cdonts.htm">Send another message</A><BR>
</BODY>
</HTML>
If we sort
through the code in Listing B, the core CDONTS processing takes
place in the following code snippet:
<%
`=== Assign variables
Dim objMsg, strFrom, strTo, strSubject, _
strBody, lngImportance
strFrom = (Request.Form("txtFrom"))
strTo = Trim(Request.Form("txtTo"))
strSubject = Trim(Request.Form _
("txtSubject"))
strBody = Trim(Request.Formn _
("txtMessage"))
lngImportance = Trim(Request _
("optImportance"))
Set objMsg = Server.CreateObject _
("CDONTS.NewMail")
objMsg.From = strFrom
objMsg.To = strTo
objMsg.Subject = strSubject
objMsg.Body = strBody
objMsg.Importance = lngImportance
objMsg.Send
Set objMsg = Nothing
%>
This code
snippet uses the Request.Form object to assign variables into
the To, From, Subject, Importance, and Body of the message.
Next, the CDONTS object is instantiated with the Server.CreateObject
method:
Set objMsg = Server.CreateObject _
("CDONTS.NewMail")
Now that
an instance of the object exists and the object's properties
are set, the message is sent using the send method:
objMsg.Send
You could have also sent the message using just
three lines of code:
Set objMsg = Server.CreateObject
=>("CDONTS.NewMail")
objMsg.Send strFrom, strTo, strSubject,
=>strBody, lngImportance
Set objMsg = nothing
Although the send method syntax allows all arguments to be optional,
your message isn't much good if it isn't sent to a valid SMTP
address (user@domain):
Send ([From] [, To] [, Subject] [, Body]
=>[, Importance])
Conclusion
As
you can see from our example, the CDONTS technology is straightforward,
with one quirk to keep in mind: once the CDONTS object has sent
the message, the object can't be used to send another message.
If you find yourself in a situation where you have to send multiple
messages within the same script, you'll need to create a process
to serially creating unique CDONTS objects.