Active Server Developer's Journal

June 1999

Using Advanced CDONTS Techniques

by Scot Johnson

In the May issue, we covered the basic CDONTS messaging functionality in the article, "Sending email via ASP and CDO for NT Server." The technology is simple and can be an extremely useful tool for developing enterprise applications that require Web-enabled messaging technology. In this article, we'll investigate some of the additional features of CDO for NTS.

Getting started: collecting data

To get started, we'll going to use an user input page to collect data for our example, as shown in Figure A. As you can see, cdonts_advInput.htm, gives the user similar messaging capabilities found in most email packages today. These features include the ability to copy, blind-copy, use a reply-to address, send file attachments, set the message format type, and specify the encoding method. Keep in mind, that is page is not required to use CDO for NTSyou can collect the required information from any sourcea database feed, Session variables, etc. We'll use cdonts_advInput.htm, as shown in Figure A represent a message with a file attachment, and coping and blind-copied another user. When the recipients first check their email in Outlook 2000, they'll see the message and a file attachment, as shown in Figure B.

Figure A: These are additional CDO for NTS input features.

Figure B: This is the message the user will see when he first checks his email.

Again, let's look into the details of what makes this ASP review the NewMail properties and methods. The source code for cdonts_advInput.htm, shown in Listing A, uses HTML Form objects to collect input from the user.

Listing A: cdonts_advInput.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_advInput.asp" 
ID=FORM1 NAME="FrontPage_Form1" 
ONSUBMIT="return FrontPage_Form1_Validator(this)">
<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 	
SIZE="45" ></TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left">Cc:</TD>
<TD VALIGN="top" ALIGN="left">
<INPUT NAME="txtCc" SIZE="45"></TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left">Bcc:</TD>
<TD VALIGN="top" ALIGN="left">
<INPUT NAME="txtBcc" 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">
Reply To:</TD>
<TD VALIGN="top" ALIGN="left">
<INPUT NAME="txtReplyTo" 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&nbsp;&nbsp;&nbsp;
<INPUT TYPE="radio" 
CHECKED NAME="optImportance" 
VALUE="1">
Normal&nbsp;&nbsp;&nbsp;
<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 
or HTML format

To use HTML in the body of your message, make sure 
to select HTML Body Type and MIME 		
Encoding</TEXTAREA></TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left">
Body Type:</TD>
<TD VALIGN="top" ALIGN="left">
<INPUT TYPE="radio" NAME="optMsgType" VALUE="1" 
TABINDEX="1" checked>
MIME&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT TYPE="radio" 
NAME="optMsgType" VALUE="0" TABINDEX="2">HTML</TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left">
Encoding:</TD>
<TD VALIGN="top" ALIGN="left">
<INPUT TYPE="radio" NAME="optMsgEncode"
VALUE="0">MIME&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT TYPE="radio" 
NAME="optMsgEncode" VALUE="1" checked>TEXT</TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left" 
COLSPAN="2"><FONT FACE="MS Sans Serif" 
SIZE=1>Attach File:&nbsp;
<INPUT NAME="txtattfile" TYPE="file" 
SIZE="55"></FONT></TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left" 
COLSPAN="2"><FONT FACE="MS Sans Serif" 
size="1">Encode
Attachment: </FONT>
<INPUT TYPE="radio" 
NAME="optAttEncode" VALUE="0" checked>UUENCODE
<INPUT TYPE="radio" 
NAME="optAttEncode" VALUE="2">
<FONT FACE="MS Sans Serif"
size=1>Base 64</FONT></TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left" 
COLSPAN="2">
<P ALIGN="center">
<INPUT TYPE="submit" VALUE="Send Message" NAME="btnSend"
TABINDEX="1">&nbsp;&nbsp;&nbsp;
<INPUT TYPE="reset" VALUE="  
Clear  " NAME="btnClear" TABINDEX="2"></P></TD>
</TR>
</TABLE>
</FORM>
</BODY>

</HTML>

We'll use two files in the examplean HTM file to present the user input and an ASP file to process the request. The cdonts_advInput.htm page posts the data into cdonts_advInput.asp for processing the advanced features, as shown in Listing B. The page cdonts_advInput.asp is responsible for processing the CDONTS request, and is used to display the submitted results back to the user.

Listing B: cdonts_advInput.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, 
strCc, strBcc, strReplyTo, strBody, _
	strSubject, strFileName
Dim lngImportance, lngMsgFormat, 
lngMsgEncode, lngAttEncode

strFrom = Trim(Request.Form("txtFrom"))
strTo = Trim(Request.Form("txtTo"))
strCc = Trim(Request.Form("txtCc"))
strBcc = Trim(Request.Form("txtBcc"))
strReplyTo = Trim(Request.Form("txtReplyTo"))
strSubject = Trim(Request.Form("txtSubject"))
strBody = Trim(Request.Form("txtMessage"))
lngImportance = Trim(Request("optImportance"))	
lngMsgFormat = Trim(Request("optMsgType"))
lngMsgEncode = Trim(Request("optMsgEncode"))
lngAttEncode = Trim(Request("optAttEncode"))
strFileName = Trim(Request.Form("txtattfile"))

Set objMsg = Server.CreateObject("CDONTS.NewMail")
If Len(Trim(strReplyTo)) > 0 Then
	objMsg.Value("Reply-To")=strReplyTo
End If

objMsg.From = strFrom
objMsg.To = strTo
objMsg.Cc = strCc
ObjMsg.Bcc = strBcc
objMsg.Subject = strSubject
objMsg.Importance = lngImportance
objMsg.BodyFormat = lngMsgFormat
objMsg.MailFormat = lngMsgEncode
objMsg.Body = strBody
If Len(Trim(strFileName)) > 0 Then
	objMsg.AttachFile strFileName, , lngAttEncode
End If

objMsg.Send 
Set objMsg = Nothing

WriteHTML("The following message was sent via 
CDO for NTS:")
WriteHTML("From: " &strFrom)
WriteHTML("To: " &strTo)
WriteHTML("Cc: " &strCc)
WriteHTML("Bcc: " &strBcc)
WriteHTML("Reply To: " &strReplyTo)
WriteHTML("Subject: " &strSubject)
WriteHTML("Body: " &strBody)
WriteHTML("Importance: " &lngImportance)
WriteHTML("Message Format: " &lngMsgFormat)
WriteHTML("Message Encode: " &lngMsgEncode)
WriteHTML("File Attachment: " &strFileName)
WriteHTML("File Attachment Encode: " &lngAttEncode)

%>
<HR>
Send another message with <A HREF = 
"cdonts_advInput.htm">
	advanced features</A><BR>
Send another message with <A HREF = 
"cdonts_simpleInput.htm">
	basic features</A>

</BODY>
</HTML>

You'll notice that we've built from the same functionality found in the basic CDONTS processing page, but have now included additional NewMail properties and methods object. One obvious addition is the ability to send attachments using the AttachFile method. The AttachFile requires the syntax:

objNewMail.AttachFile(Source _ 
	[,FileName] [, EncodingMethod] )
Where objNewMail is the instantiater CDO for NTS object, Source is the path and filename of the object to be attached, FileName is the name to appear in the attachment pane, and EncodingMethod enables UUEncode or base 64 format. The following code snippet illustrates attaching a file with variable strFileName for the Source locations, no filename, and a variable called lngAttEncode:
If Len(Trim(strFileName)) > 0 Then
	objMsg.AttachFile strFileName, , _
		lngAttEncode
End If

Note that if no filename is specified, the SourceFile name would display as the attachment name, as shown in Figure C. Another noticeable addition to the basic messaging code example is the ability to customize headers using the Values property. One common header, Reply-To, is used to allow the To field to be auto populated when the user clicks Reply. The following code snippet illustrates populating the To field:

objMsg.Value("Reply-To")=strReplyTo

Figure C demonstrates the result of populating the strReplyTo variable with the helpdesk@i3solutions.com address. Finally, CDONTS allows you to set the formatting and encoding properties of the message. For example, if you want to add an HTML graphical presentation to your messages, you can embed HTML tags within the body of your message and set the mail format to Multipurpose Internet Mail Extensions (MIME) encoding. When the user views the email, the MIME-compliant email will display the email as a Web browser. Figure D illustrates pasting HTML into the body of the message and setting the appropriate Body Type and Encoding rules.

Figure C:The CDONTS Value Property allows custom header information.

Figure D: Send HTML within the Body of the message with MIME Encoding.

Once this message is sent, a MIME compliant email client will display the message shown in Figure E. If you didn't want to display interpreted HTML tags in its Web display format, simply encode the message in Text format to show the HTML tags.

Figure E: If you don't want to display interpreted HTML tags in Web display format, simply encode the message in Text format.

Conclusion

One of the hurdles to overcome with CDO is simply learning the specifics of the CDONTS component library. As you can see from the examples in this article, the technology itself is fairly straightforward and can prove to be an extremely useful and powerful tool when developing enterprise applications that require Web-enabled messaging technology.

Copyright © 1999, ZD Inc. All rights reserved. ZD Journals and the ZD Journals logo are trademarks of ZD Inc. Reproduction in whole or in part in any form or medium without express written permission of ZD Inc. is prohibited. All other product names and logos are trademarks or registered trademarks of their respective owners.