Using the CDONTS component to send email from ASP pages.

Many ASP programmers want to now know to do this and with the arrival of IIS4 and the SMTP service of Option Pack 4 it is now fairly easy. There is a component that is made available after the server installation of SMTP service. It is called CDONTS and it makes sending email rather easy. Setting up the SMTP service properly is up to you. It is fairly self explanatory and I have never had any problems installing it. Any quality ISP's who offer ASP hosting should already have this set up on their servers.

Sending Mail from ASP with CDONTS.NewMail Object

The NewMail object gives you the ability to send a message within an ASP page with only few lines of code. The syntax for sending mail with the NewMail object of CDONTS is as follows:

objNewMail.Send(  [From]  [, To]  [, Subject]  [, Body]  )

You can create the instance of NewMail Object with the following code.

  <%
  Dim objNewMail
  Set objNewMail = Server.CreateObject("CDONTS.NewMail")
  %>
  
Properties of NewMail Object.
From A string value contaning the email address of the sender
(for example Me@somewhere.com)
To A string value contaning the email address of the recipient.
(for example, someone@somewhere.com)
Multiple addresses can be added by seperating wih ";"
Subject The subject line for the message.
Body A string value contaning the body text of message.
Cc A string contaning the email addresss of recipents who will recieve a copy of current message.
Bcc A string contaning the email addresss of recipents who will recieve a blind copy of current message.
Importance This is an Integer value that represents the priority of message.
(for example High, Normal or Low) 0=Low 1=Normal (default) 2=Highs
BodyFormat An integer value which sets the text format of NewMail Object.
ObjMail.BodyFormat = 0 (HTML format)
ObjMail.BodyFormat = 1 (default Plain Text format)
MailFormat An integer value which sets the encoding of NewMail Object.
ObjMail.MailFormat = 0 (Mime format)
ObjMail.MailFormat = 1 (default Plain Text format)




Methods of NewMail Object.
AttachFile This method attaches a file to the current mesage.
AttachURL This method attach a file to current message and associate a URL with that attachment.
Send This method sends the message.

After the send method the NewMail object becomes invalid but stays in the memory.
You should set NewMail Object to nothing and relase the memory after the Send method.
After the send method you will have to create a new instance of NewMail Object if you want to send another message.

Let us see how we can use CDONTS.Newmail Object to send mail from an ASP Page.

<%
Option Explicit

Dim objNewMail

' First create an instance of NewMail Object
Set objNewMail = Server.CreateObject("CDONTS.NewMail")

' After an instance of NewMail Object has been created.
' If you like you can use this one line of code to send the mail.
' objNewMail.Send From, To, Subject, Message
' or you can give every value seperate

objNewMail.From = "webmaster@
yourdomain.com"
objNewMail.To   = "test@
yourdomain.com
"

' Please replace the "From" and "To" email addresses with your
' own valid email address. I recieve too many emails
' from people who test this sample and keep sending
' emails to test@
yourdomain.com, or they keep the "From" property
' as webmaster@
yourdomain.com and I get response of
' undeliverable emails.
' NOTE: If the "To" or "From" properties of CDONTS contain
' invalid email address you will not recieve the email.

objNewMail.Subject = "This is a test Mail"
objNewMail.Body    = "This is the Body text of this test mail."
objNewMail.Send

' After the Send method, NewMail Object become Invalid
' You should set it to nothing to relase the memory

Set objNewMail = Nothing

' If you want to send another mail
' you have to create a new instance of NewMail Object again.

Response.Write "Email has been sent"

%>


Setting the Reply-To Header in an Email using CDONTS.NewMail Object

This Sample Code will help you to understand how you can set the Reply-To header of an email message using the CDONTS.NewMail object in an ASP Page.

Note : We won't support from id other than your domain name . i.e. @yourdomainname.com ( not supported @yahoo or @hotmail )
<%
Option Explicit

Dim objMail
Dim strSubject
Dim strBody

strSubject = "This is a test email"
strBody    = "This test email is using test@
yourdomain.com " & _
           " as the sender email address but we are " & _
           " using someone@
yourdomain.com
as the Reply-To header."

     
' First create an instance of NewMail Object
Set objMail = Server.CreateObject("CDONTS.NewMail")

' Please replace the "From" email addresses with your
' own valid email address.
we recieve too many emails
' from people who test this sample and keep sending
' emails to test@
yourdomain.com
' NOTE: If the "To" or "From" properties of CDONTS contain
' invalid email address you will not recieve the email.
 
objMail.From = "test@
yourdomain.com
"
objMail.To   = "
client@clientdomain.com
"

' Use the value property of the CDONTS.NewMail Object
' to Set the Reply-To header

objMail.Value("Reply-To") = "someone@
yourdomain.com
"

objMail.Subject = strSubject
objMail.Body    = strBody

objMail.Send

Set objMail = Nothing
%>


Sending Email in HTML format using CDONTS.NewMail Object

This Sample code will help you understand how you can send a HTML formated email from an ASP page using the CDONTS.NewMail object.


<%
Option Explicit

Dim objMail
Dim strSubject
Dim strBody

strSubject = "This is a test email in HTML format"

strBody = "<HTML>" & _
     "<HEAD></HEAD>" & _
     "<BODY>" & _
     " <Font Face=Arial Size=5><B>" & _
     "  This is a test Email" & _
     " </B></Font><BR>" & _
     " <H3><A Href=http://www.
yourdomain.com>Click here</a>" & _
     " to go to
yourdomain.com
</h3>" & _
     "</BODY>" & _
     "</HTML>"

' First create an instance of the NewMail Object
Set objMail = Server.CreateObject("CDONTS.NewMail")

' Please replace the "From" and "To" email addresses with your
' own valid email address. I recieve too many emails
' from people who test this sample and keep sending
' emails to test@
yourdomain.com, or they keep the "From" property
' as webmaster@
yourdomain.com and I get response of
' undeliverable emails.
' NOTE: If the "To" or "From" properties of CDONTS contain
' invalid email address you will not recieve the email.

objMail.From    = "test@
yourdomain.com
"
objMail.To      = "someone@
yourdomain.com
"
objMail.Subject = strSubject
objMail.Body    = strBody

' In order to send the email in HTML format we have to set
' both MailFormat and BodyFormat equal to zero

objMail.MailFormat = 0
objMail.BodyFormat = 0

objMail.Send

Set objMail = Nothing
%>

 


The following three examples should give you the basic idea and get you started.


Using CDONTS to send a text based email message.

<%
    Dim MyBody

    Dim MyCDONTSMail
%>


<%
    Set MyCDONTSMail = CreateObject("CDONTS.NewMail")
    MyCDONTSMail.From= "somebody@nowhere.com"
    MyCDONTSMail.To= "nobody@nowhere.com"
    MyCDONTSMail.Subject="This is a Test"
    MyBody = "Thank you for ordering that stuff" & vbCrLf
    MyBody = MyBody & "We appreciate your business" & vbCrLf
    MyBody = MyBody & "Your stuff will arrive within 7 business days"
    MyCDONTSMail.Body= MyBody
    MyCDONTSMail.Send
    set MyCDONTSMail=nothing
%> 


Using CDONTS to send a HTML based email message.

You'll get the general idea..notice how you have to add extra double quotes inside the HTML. You can use any valid HTML. Just make sure you link the images back to your webserver so that the recipient doesn't get a broken image. If the recipient's email program doesn't support HTML this is not always a good way to send email, but most do in my experience and if they don't they will still get the message... it will just look kinda strange.

<%
    Dim MyCDONTSMail2
    Dim HTML
    Set MyCDONTSMail2 = CreateObject("CDONTS.NewMail")
    HTML = "<!DOCTYPE HTML PUBLIC""-//IETF//DTD HTML//EN"">"
    HTML = HTML & "<html>"
    HTML = HTML & "<head>"
    HTML = HTML & "<title>Sending CDONTS Email Using HTML</title>"
    HTML = HTML & "</head>"
    HTML = HTML & "<body bgcolor=""FFFFFF"">"
    HTML = HTML & "<p><font size =""3"" face=""Arial""><strong>"
    HTML = HTML & "Name Of Store</strong><br>"
    HTML = HTML & "Incoming Customer Order</strong></p>"
    HTML = HTML & "<p align = ""center"">Bla Bla Bla Bla Bla</p>"
    HTML = HTML & "</body>"
    HTML = HTML & "</html>"
    MyCDONTSMail2.From= "somebody@somewhere.com"
    MyCDONTSMail2.To="nobody@somewhere.com"
    MyCDONTSMail2.Subject="Incoming Customer Order"
    MyCDONTSMail2.BodyFormat=0
    MyCDONTSMail2.MailFormat=0
    MyCDONTSMail2.Body=HTML
    MyCDONTSMail2.Send
    set MyCDONTSMail2=nothing
%>


Using CDONTS to send a file attachment and have a Carbon Copy Recipient.

<%
    Dim MyBody2
    Dim MyCDONTSMail3
%>


<%
    Set MyCDONTSMail3 = CreateObject("CDONTS.NewMail")
    MyCDONTSMail3.From= "somebody@nowhere.com"
    MyCDONTSMail3.To= "nobody@nowhere.com"
    MyCDONTSMail3.Cc="nobody2@nowhere.com"
    MyCDONTSMail3.Subject="This is a Test"

   
    MyCDONTSMail3.AttachFile Server.MapPath("/somedirectory/bla.txt")
    ' or you could specify the path exactly if you knew it like below
    'MyCDONTSMail3.AttachFile ("C:\inetpub\wwwroot\somedirectory\bla.txt")

    
    MyBody2 = "Thank you for ordering that stuff" & vbCrLf
    MyBody2 = MyBody2 & "We appreciate your business" & vbCrLf
    MyBody2 = MyBody2 & "Your stuff will arrive within 7 business days"
    MyCDONTSMail3.Body= MyBody2
    MyCDONTSMail3.Send
    set MyCDONTSMail3=nothing
%>

When sending an attachment this is just a very simple example. There are certain encoding options and file type settings that you may also want to set.

Quick Note: If you get an error and you have. "Run in separate process" checked under your Application settings in the IIS Console try turning that off for that web or search the Online Microsoft Knowledge Base for a description of the fix. The fix for making CDONTS work with "Run in separate process" checked is a catch 22 situation involving security risks. Unfortunately I can longer find that article and can not tell you how to do it since I don't really remember it at all.

Final Note: If you have problems setting up SMTP and CDONTS or are worried about security and your Metabase or whatever else, please don't email me about that. I am simply showing you working examples of how to use the code, these examples have all been tested.