Home
Link To Us
Components
Resources










Site Menu

An Overview in Utilizing ASP to Provide Database Access Through Web Browsers

ASP Resource and Material Library

Search the Web for other ASP Tutorials

Available ActiveX Components and Source Code

A good reference guide to ASP and COM Objects
cover
More CDO Resources











An Overview in Sending EMail from ASP Scripts Using CDO
by Derrald Farnsworth-Livingston

Technical Setup Stuff

In this document we will discuss how to write an ASP Script to send emails. There are many benefits to this. For instance, perhaps you have a mailing list (like tutorial-web) that you want to have people be able to sign up for. However, when they have successfully inputted their email address you would like them to receive a confirmation. In addition, this can be an added check for you. If you receive a non-delivery message from the server than you know that the email address is bogus.

Technical Setup Stuff

The first thing is we must have a SMTP server through which we can relay email. Internet Information Server 4.0 and higher comes with such a service we can install. We must check to make sure that the relay settings are enabled. We can do this by:
1. Open the IIS Manager on the desired Server
2. Choose a SMTP Server (the default is Default SMTP Virutal Server)
3. Right-click on that Server and choose properties
4. Choose the "Access" tab
5. Click on "Relay..."
6. If the Selection "Only the List" below is enabled then click "Add..." otherwise make sure the server you wish to add is not in the list
7. Add the IP Address of the web server that will run the Email Script

Code, Code, Code

We shall begin by setting up a variable to be associated with the CDONTS.Newmail object in the ASP Script. This can be accomplished by way of the Createobject command.

    Set MyCDONTSMail = CreateObject("CDONTS.NewMail")

Once we have set up the Object we now need to associate the properties with the values of the email we want to send.

The From, To, Subject, CC, BCC, Body are all read/write properties we can set values to.

	MyCDONTSMail.From = "Joe@Joe.com"
	MyCDONTSMail.To= "Steve@Steve.Net"
   	MyCDONTSMail.Subject="Hello Steve!"
	MyCDONTSMail.Body= "It's good to send you an email, Steve!"

Once we have associated all the properties we wish to set we can then call the Send Method which actually sends the data.

    MyCDONTSMail.Send

And we dissassociate the variable to clean up.

    set MyCDONTSMail=nothing

Of course, the properties could have just as easily been set to variables, or database fields. If you want to send to everyone in a database, a simple loop could address each email with the variables you desire. For instance, the following code is what drives the mailing list subscribtion on this very site. As you can see, I have the To property set to the variable "subscriberemail" and I have the subscribername, starting off the Message Body.

    Set MyCDONTSMail = CreateObject("CDONTS.NewMail")
    MyCDONTSMail.From= "webmaster@tutorial-web.com"
    MyCDONTSMail.To= subscriberemail
    MyCDONTSMail.Subject="Welcome to Tutorial-Web"
    MyBody = subscribername & ", Thank You for joining Tutorial-Web,"
    MyBody = MyBody &  & vbCrLf & vbCrLf
    MyBody = MyBody & you will be notified of updates."
    MyBody = MyBody & "Your information will NOT be sold to anyone."
    MyBody = MyBody & vbCrLf
    MyBody = MyBody & "If you have joined accidently or would"
    MyBody = MyBody & " like to unsubscribe please send an"
    MyBody = MyBody & " email to webmaster@tutorial-web.com with a" 
    MyBody = MyBody & subject of 'Please Remove from List'" & vbCrLf
    MyCDONTSMail.Body= MyBody
    MyCDONTSMail.Send
    set MyCDONTSMail=nothing

The CDO object is a nice way to add a little more functionality to your website without adding a component or creating long scripts. Dropping in a mailing list subscription box, or a sign-up sheet can spice up any website.


Sections

Sending EMail Using CDO

The Bookworm
Search the Tutorial-Web library for books on any topic!

Sponsors

Search Now:
In Association with Amazon.com