Digital Colony!

Sending Email Using CDONTS

ASP developers can use the CDONTS technology, which is built into Microsoft's IIS web server to send server-side email. CDONTS, which is also referred to as CDO, stands for Collaboration Data Objects. Once an SMTP service is setup, CDONTS allows the sending of email from ASP script. Setting up an SMTP service is outside the scope of this article.

Basic Text Email

The simplest email is the plain text message. Below, is sample code for sending a plain text email. The following example will send a message to Huey, carbon-copy Louie, and blind-carbon-copy Dewey.
on error resume next 
Set Mailer = Server.CreateObject("CDONTS.NewMail") 
Mailer.From = "Duck Newsletter<newsletter@duck.com>" 
Mailer.To = "huey@duck.com" 
Mailer.CC = "louie@duck.com" 
Mailer.BCC= "dewey@duck.com" 
Mailer.Subject = "Duck Newsletter" 
mailBody = "-- newsletter text --" 
Mailer.Body = mailBody 
Mailer.Send 
If err.num Then    
    Response.Write "CDONTS Error: " & err.num & " - " & err.description
End If

Wrapping it up with HTML

Sending HTML email is just another variation on text email, since HTML is merely marked up text. Other than the HTML tags, the only difference is specifying a valid DOCTYPE in the first line of the email. There a few valid DOCTYPES to chose from when constructing an HTML email. Web Design Group has put out a good article that describes the differences: Choosing a DOCTYPE. It also advised to include a META tag describing the Content-Type. In this example the Content-Type is Latin1 HTML.
on error resume next 
Set Mailer = Server.CreateObject("CDONTS.NewMail") 
Mailer.From= "Duck Newsletter<newsletter@duck.com>" 
Mailer.To = "huey@duck.com" 
Mailer.Subject = "Duck Newsletter" 
' BodyFormat and MailFormat should be set to 0 for HTML emails. 
Mailer.MailFormat = 0 
Mailer.BodyFormat = 0 
Dim mailBody
' 1-add DOCTYPE to mailBody
' 2-add HTML HEAD TITLE and BODY tags to mailBody
' 3-construct message using valid HTML inside mailBody
' 4-close BODY and HTML tags to mailBody
Mailer.Body = mailBody 
Mailer.Send 
If err.num Then    
     Response.Write "CDONTS Error: " & err.num & " - " & err.description 
End If

More CDONTS Features

You may want to use CDONTS for two other things: sending a file attachment and assigning a message priority. You can also provide an optional 2nd parameter to name the file attachment. Defining priority is done via the Importance property. The three posible values are: High (2), Normal (1), or Low (0). The default is Normal. This example will send a file attachment and mark the message as HIGH priority.

Last Words

CDONTS is a very robust way to send server-side email. I've used it to send 1000 newsletters on 1 ASP page without error. If you plan to use CDONTS to send a large number of newsletters from a single page, be sure to add "Server.ScriptTimeout = 99999999" at the top of the ASP page. This will prevent IIS from timing out the page while the messages are being sent.

This article was originally written in 2001.

Labels: ,

AddThis Social Bookmark Button

0 Comments:

 

Post a Comment

 

Digital Colony Copyright © 1999-2008 XHTML   508
This site uses Blogger, which is not 100% XHTML compliant.
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.