Handling email is a very important part of every active website.
Automating mail delivery could be extremely useful and could have many uses such as:
- Online order verification.
- Creating a mailing list to update customers of recent news/sales.
- Responding to a software download.
- And the list goes on...
The reasons may vary, but the need is the same: your site should be able to send email automatically.
And this is exactly what you're going to learn in this article - using the CDONTS library to send email.
Important notice: the CDONTS library is only applicable to NT users, it will not work for PWS users.
The Importance of Automated Mail Response
Just a moment before we delve into the techy stuff, I'd like to emphasize the importance of an automated mail response.
When a user gets an email notification from your site he gets the feeling that someone has answered him, he has someone to
talk to, this isn't yet another dead-end sale. The response to the user's action gives him a sense of security.
Automated mail response is an important tool - use it wisely.
Let the learning begin!
The SMTP Service
SMTP - Simple Mail Transfer Protocol - is a Service on the NT OS. It is installed with the IIS (if you choose it
from the list of components).
How can you know whether it is installed on your server or not? Simple. Call it and see if you get an answer!
Choose Start > Run from the Start menu and type
telnet [ServerName] 25.
ServerName with your server name of course. 25 is the port number SMTP listens to.
If it's installed you should get an answer similar to this:
220-MyServer Microsoft SMTP MAIL ready at
Sun, 24 March 2002 18:12:21 - 0500 Version 5.5.1877.977.9
220 WSMTP Spoken here
If not, then you probably don't have SMTP installed on your server. To install it run the NT Option Pack installation,
and choose the appropriate option.
The CDONTS library - Collaboration Data Objects for NT Server, which also comes with the NT Option Pack, provides
very useful objects for delivering/receiving mail through SMTP. Note that there is another library called CDO, which is much
richer and provides a lot more options. It comes with the Microsoft Exchange Server. The CDONTS library is simpler to use, and with
it you can send mail within a few lines of code.
Lets get on with it already!
The NewMail Object
The CDONTS library contains a number of objects, but we will focus on the simplest object:
NewMail. It is used for
quick and simple mail delivery.
Lets quickly review the
NewMail object properties:
|Blind Carbon Copy (additional delivery address)|
|The message body|
|Message format: 0-HTML 1-Text|
|Carbon Copy (additional delivery address)|
|Base path for files in the message|
|Relative base path for files in the message|
|Priority: 0 to 3|
|Delivery method: 0-Mime 1-Text|
|Additional message headline|
There are 3 options to add receivers' addresses:
To - the simplest way, this is the primary receiver address.
CC - additional receiver (the receiver can see who else received the message).
BCC - additional receiver (the receiver can't see who else received the message, it will look as if it
was sent only to him).
Now lets review the
NewMail object methods:
|Attach a file to the message|
|Attach URL to the message|
|Send the message|
|Set a Code Page for the message|
Sending the Mail!
Ok, we've come this far now lets send a message!
Create myMail - a
Set myMail = Server.CreateObject("CDONTS.NewMail")
Now set the message properties: receiver's address, sender's address, message content, subject and so on.
myMail.To = "email@example.com"
myMail.Body = "Hey Bill, how come you're using yahoo mail service?"
myMail.From = "faq@your_company.com"
myMail.Subject = "Testing"
Set the message format to MIME (Multipurpose Internet Mail Extension):
Add a file attachment:
Call myMail.AttachFile("\\server\bill_photos\bill_wife_nude.jpg", "BILL_WIFE_NUDE.JPG")
Send it already!
Now don't forget to clear the object...
Cut! And we're done.
So, what have we done so far?
- We discussed the importance of automated mail response.
- We got the SMTP Service up and running.
- We reviewed the
NewMail object from the CDONTS library.
- And we sent out a simple message, though I assume you'll improve the code to suit your needs.
Wrap it up guys.