Sending HTML with CDO
By Alan Saldanha
Introduction
IIS 4.0 provides an interface to send e-mail via SMTP or the Exchange Server. This interface is called CDO and can be used from an Active Server Page. In March, we published an article by Jon Whiting that outlines the basics of sending e-mail with CDO, entitled: "Collaboration Data Object and IIS 4.0".
In addition to sending plain text mail, you can use the CDO object to send MIME HTML messages and attachments using the AttachURL method. Using AttachURL, you can send e-mail that looks, in Outlook Express, like Figure 1.
Figure 1 : MIME HTML E-mail in Outlook Express
Click Here to View Figure
When you click on "Apr 8, 1998 - New Vendor Site", Internet Explorer will open and navigate to the page indicated in the link. Figure 2 shows the browser after following the link from Figure 1.
Figure 2 : Internet Explorer
Click Here to View Figure
Sending HTML E-mail
The following snippet of script was used to send the e-mail message in Figure 1.
<%
Dim myMail
Set myMail = CreateObject("CDONTS.NewMail")
myMail.AttachURL "c:\images\logo.gif", "logo.gif"
myMail.AttachURL "c:\images\new.gif", "new.gif"
HTML = "<!DOCTYPE HTML PUBLIC ""-//IETF//DTD HTML//EN"">" & NL
HTML = HTML & "<html>"
HTML = HTML & "<head>"
HTML = HTML & "<meta http-equiv=""Content-Type"""
HTML = HTML & "content=""text/html; charset=iso-8859-1"">"
HTML = HTML & "<title>15 Seconds</title>"
HTML = HTML & "</head>"
HTML = HTML & "<body bgcolor=""#FFFFFF"">"
HTML = HTML & "<p><font face=""Arial Black""><strong>"
HTML = HTML & "<img src=logo.gif><br>"
HTML = HTML & "<img src=new.gif>"
HTML = HTML & "<a href=http://vendors.15seconds.com>"
HTML = HTML & "Apr 8, 1998 - New Vendor Site</a>"
HTML = HTML & "</strong></font></p>"
HTML = HTML & "<p>15 Seconds is proud to announce a new site"
HTML = HTML & "dedicated to component vendors. The 15 Seconds'"
HTML = HTML & "Vendor Site will help vendors, design, develop and"
HTML = HTML & "marketing their Active Server components. The"
HTML = HTML & "premier article for the new vendor site is entitled:"
HTML = HTML & """Twelve Steps to Becoming a Component Vendor."""
Using Images
A directory was used to hold the images. In the script, the directory images (c:\images) was used to store the images (logo.gif and new.gif). You must know the physical path of the images on the web server. When the e-mail is sent, the images are also sent with the e-mail.
To attach the images in the e-mail you need use the AttachURL method of the CDO object. The actual path of an image was referenced in the AttachURL method. For example in:
myMail.AttachURL "c:\images\logo.gif", "logo.gif"
logo.gif refers to the image at c:\images\logo.gif. The alias, in this case 搇ogo.gif? is used throughout the rest of the HTML page as illustrated by:
HTML = HTML & "<img src=logo.gif><br>"
Including HTML
The body of an e-mail message can contain either plain text or HTML. The BodyFormat property specifies whether or not any HTML is included. If BodyFormat is set to 0, the Body property of the CDO object can include HTML. If set to 1, it can contain only plain text. In the script, the BodyFormat is set to 0:
myMail.BodyFormat = 0
An e-mail message can be plain text or in MIME format. The MailFormat property needs to be set to 0 for MIME format or 1 for plain text. In the script, the MailFormat is set to 0:
MyMail.MailFormat = 0
Conclusion
It is important to understand that not all e-mail clients display HTML e-mail messages. The HTML e-mail works just great with Microsoft抯 Outlook Express. However, the images were not displayed when the e-mail was read using Netscape抯 Communicator.
The CDO object has a lot to offer if you are looking to develop HTML-based e-mail. Combined with javascript:if(confirm('http://www.serverobjects.com/ \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.serverobjects.com/'" tppabs="http://www.serverobjects.com/">Server Object抯 ASPImage and ASPChart you could send customized e-mail with personalized graphics to every subscriber to your Web site or every employee, in the case of an intranet.
Additional Resources
Collaboration Data Object and IIS 4.0
Additional Articles by Alan
ASP On Speed
How To Track User Activity At A Web Site Using ASP
ASP 101 ?A Database Interfacing Primer
Copyright 1999-2000 internet.com Corp. All RIGHTS RESERVED.
给你个通用函数
sub SendMail(fromWho,toWho,Subject,Body)
dim myMail
SET myMail=server.CreateObject ("CDONTS.Newmail")
with myMail
.MailFormat =0
.BodyFormat =0
.From =fromWho
.To =toWho
.Subject =Subject
.Body =Body
.Send
end with
<%
dim objMail
set objMail=server.CreateObject("Cdonts.NewMail")
objMail.From="kingshang@hotmail.com"
objMail.To="youname@hotmail.com"
objMail.Subject="test Mail"
objMail.Body="Hello,This is just a test Mail."
objMail.Send
Response.Write("Mail has been sent.")
%>