Collaboration Data Object and IIS 4.0
By Jon Whiting
Introduction
Collaboration Data Objects (CDO) is a COM library designed to send mail through SMTP (Simple Mail Transfer Protocol) or Microsoft Exchange. If you install the SMTP server that comes with Microsoft Option Pack 4, you can send mail from an Active Server page using CDO. Because CDO comes with Microsoft Option Pack 4, CDO is free.
|
Sending Mail
It only requires three lines of code to send e-mail from an Active Server page using CDO, as shown in Example 1.
Example 1
Set objMail = Server.CreateObject("CDONTS.NewMail")
objMail.Send "fromuser@domain.com","touser@domain.com","Test Mail",
"This is a test mail using CDO",0
Set objMail = nothing
When you create the Newmail object, it actually creates a CDO Session, and logs into the SMTP server that is installed on the same system as the Web Server. You must install Microsoft’s SMTP server that comes with NT Option Pack 4. There is no way to change the SMTP server that the CDO object uses.
After the e-mail is sent, the Newmail object becomes invalid, so you need to make sure to set the object to Nothing. To send another piece of e-mail, you must recreate the Newmail object.
There is no SMTP error checking done in the Newmail object. The object submits the e-mail to the SMTP server where it is sent or rejected. The script running the object does not get an error code from the object since the process takes place on the SMTP server. Any delivery errors in the e-mail will be forwarded from the SMTP server to the From (that is, the sender’s) e-mail address.
There are two ways to send e-mail using the Newmail object. The first, demonstrated in Example 1, uses the Send method, passing parameters with it. The second way is to separate the parameters out, and set each of them individually. Example 2 demonstrates the setting of individual parameters:
Example 2
Set objMail = Server.CreateObject("CDONTS.NewMail")
objMail.From="user@domain.com"
objMail.To="user@domain.com"
objMail.Subject="Test Mail"
objMail.Body="This is a test mail using CDO"
objMail.importance=0
objMail.Send
Set objMail = nothing
If you want to change the priority on the e-mail, set the importance attribute to one of the settings listed below:
In example 2, the importance was set to low.
|
Attaching a File
You can use the Newmail Object to attach a file to the e-mail message. To attach a file, you call the AttachFile method on the Newmail object. You need to specify the file to attach and, if you want to rename the file, a new name for the file.
Example 3: Attaching a File
Set objMail = Server.CreateObject("CDONTS.NewMail")
objMail.attachFile("\\server\share\file.txt","test.txt")
objMail.Send("user@Domain.com","user@Domain.com","Test Mail",
"This is a test mail using CDO",0)
Set objMail = nothing
|
Sending Mail from a Form
You can use the Newmail object to send the contents of a form to an e-mail address.
Example 4: Using a Form to send mail
<html>
<body>
<%
If request("Button")="Send Mail" then
Set objMail = Server.CreateObject("CDONTS.NewMail")
For each sRequest in Request.Form
response.write sRequest
Select Case sRequest
Case "From"
objMail.From=request(sRequest)
Case else
sBody=sBody & sRequest & " - " & request(sRequest) & chr(13)
End select
Next
response.write sBody
objMail.Body=sBody
objMail.To="user@domain.com"
objMail.Subject="Results from a form"
objMail.Send
set objMail = nothing
response.write "Mail Sent"
else
%>
<form action="FormMail.asp" method="POST" name="CDOMail">
Name: <input type="text" size="20" name="Name"><br>
Address:<input type="text" size="20" name="Address"><br>
City: <input type="text" size="20" name="City"><br>
State: <input type="text" size="20" name="State"><br>
Zip: <input type="text" size="20" name="Zip"><br>
Phone: <input type="text" size="20" name="Phone"><br>
Email: <input type="text" size="20" name="From"><br>
<input type="submit" name="Button" value="Send Mail">
</form>
<%
End if
%>
</body>
</html>
|
Sending Mail to a list
You could very easily set up a mailing list. Display a form on your web site that collects e-mail addresses and saves them to a database. Use that database and the code in Example 5 to send messages to the list of people. If the SMTP server cannot send all of the e-mail as fast as you create it, the server will save the e-mail in a directory and then catch up as it has time.
Example 5: Sending to a list
Set Conn = Server.CreateObject("ADODB.Connection")
sConnect = "dsn=MailingListDB"
Conn.Open sConnect
Set rsEmails=Conn.Execute("Select EmailAddress From Users")
Do While not rsEmails.eof
Set objMail = Server.CreateObject("CDONTS.NewMail")
objMail.From="user@Domain.com"
objMail.To=rsEmails("EmailAddress")
objMail.Subject="Test Mail"
objMail.Body="This is a test mail using CDO"
objMail.importance=0
objMail.Send
Set objMail = nothing
rsEmails.movenext
loop
set rsEmails = nothing
Conn.close
Set Conn = nothing
You will have to change the DSN and SELECT statements to match your database setup. Also, if you have a big mailing list you will have to increase the script timeout value so the Active Server page has enough time to send all the people in the mailing list without timing out. Here is how to set the timeout for 300 seconds:
Server.ScriptTimeout = 300
|
|
|