SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Sending E-Mail from ASP : Addressing your Message

The NewMail object serves a single purpose, to provide a quick and easy interface to send e-mail. To keep the usage of the object simple, all the properties on the NewMail object are write-only. This means that all you can do is set the properties. Also, the object is designed for a single use - once a message is mailed, the object must be released by setting it to nothing or assigned to a new message object.

For example, to quickly send three different personalized messages, you create three instances of the NewMail object:

<%
Dim users, iuser, oMailMessage

users = Array("user1@example.com","user2@example.com","user3@example.com")
for iuser = 0 to ubound(users)
  ' Instantiate a NewMail object
  Set oMailMessage = Server.CreateObject("CDONTS.NewMail") 

  ' Send an email to to@example.com from from@example.com
  oMailMessage.Send("from@example.com", "to@example.com", "Demo Subject", _ 
                  "Dear " & users(i) & "," & chr(13) & chr(10) & "This is cool!")

  ' Release the NewMail object
  Set oMailMessage = Nothing ' Release the object
next
%>

You can also send a single e-mail message to multiple recipients as well as copy and blind copy recipients. To, CC, and BCC properties are exposed that correlate directly to the same fields in most e-mail programs. Below we send a mail message to 3 recipients, copy another, and finally blind copy one person. When you blind copy a user no other recipient knows they exist nor are replies sent to the BCC'ed person.

<%
  Dim oMailMessage

  ' Instantiate a NewMail object
  Set oMailMessage = Server.CreateObject("CDONTS.NewMail") 

  ' Send an email to to@example.com from from@example.com
  oMailMessage.from = "from@example.com"
  oMailMessage.to = "user1@example.com; user2@example.com; user3@example.com"
  oMailMessage.cc = "friend@example.com"
  oMailMessage.bcc = "boss@example.com"
  oMailMessage.subject = "Test Subject"
  oMailMessage.body = "Wow - This is cool!"
  oMailMessage.Send()

  ' Release the NewMail object
  Set oMailMessage = Nothing ' Release the object
%>

One of the popular questions we receive is how do you send HTML-based e-mail messages? Next we show you how to format your mail messages as HTML and then we conclude with how to include images with the e-mail message.