ASP Email - CDONTS

Homepage > ASP Tutorials > ASP Email - CDONTS

CDONTS (Collaboration Data Objects for NT Server) was the default mail component used in ASP for a long time, but on Windows 2003 servers it's more common to send ASP email using CDOSYS (the component that replaced it). CDONTS is still available on most servers and remains a popular way of sending email from ASP.

We start by creating a new mail object.

<%
  Set oMail = Server.CreateObject("CDONTS.NewMail")
%>

Once we've created the mail object we can add some recipients, and the senders address:

<%
  with oMail
    .From = "postmaster@yourdomain.com"
    .To = "you@domain.com"
    .CC = "someone@anotherdomain.com"
    .BCC = "anotherperson@somedomain.com"
  end with
%>

Now set the email Subject, Body text and some formatting properties. If you want to send HTML email, set the MailFormat and BodyFormat both to 0. For Plain Text, set them both to 1.

<%
  with oMail
    .Subject = "Your file..."
    .Body = "Please find the file you requested attached to this email."
    .Importance = 1 ' 0=low, 1=normal (default), 2=high
    .BodyFormat = 1 ' 0=HTML, 1=plain text (default)
    .MailFormat = 1 ' 0=MIME (use for HTML e-mail), 1=plain text (default)
  end with
%>

To attach a file to the message, use the AttachFile method as shown below.

We can also embed an image in the message for use in HTML email. Set the physical path to the image, then the filename you want to reference the image by. You can reference this image using the standard HTML <img> tag.

<%
  with oMail
    .AttachFile "C:\Inetpub\wwwroot\attachment.zip"
    .AttachURL "C:\Inetpub\wwwroot\logo.gif", "mylogo.gif"
    .Body = "Display your embedded image like this: <img src='mylogo.gif'>"
  end with
%>

When all the required properties (To, From, Body) are set, send the email by calling the Send method. Finish off by destroying the mail object:

<%
  oMail.Send
  Set oMail = Nothing
%>

Destroy the mail object and you are finished!

Download this script

Related links

Rate this tutorial!

  • Currently -8746.1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Rated -8746.1/5 stars (1143 votes cast)

Make an enquiry

Privacy policy