
|
Collaboration Data Object and IIS 4.0 By Jon Whiting |
Rating: 3.9 out of 5 Rate this article
|
email this article to a colleague
suggest an article
|
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
|
|
|
|
|
Supporting Products/Tools
|
|
AspEmail
|
|
Free SMTP component that supports multiple file attachments, unlimited recipients, CC's, BCC's and REPLY-TO's. Sends messages as plain text or in the HTML format. Premium features include message queuing and deferred processing for high mail volumes. When used with AspEncrypt, generates S/MIME-enabled secure mail.
|
[Top]
|
|
|
|
AspMail
|
|
AspMail supports multiple file attachments (MIME and UUE), US ASCII and ISO-8859-1 character sets, 8bit subject lines, custom message content headers, custom message headers, MS Exchange priority headers, PGP and more.
|
[Top]
|
|
|
DevMailer 1.0
|
|
DevMailer adds SMTP email sending abilities to ASP or Perl programs. Features include: attachments, failsafe queueing, redundant servers, standard message file support, and advanced activity logging. Also verify email addresses and send multiple messages on a single connection.
|
[Top]
|
|
|
|
JangoMail
|
|
JangoMail, located at JangoMail.com, is a web-based service that sends mass
e-mails by connecting to data from your SQL Server or ODBC compliant
database. Unlike traditional ASP e-mail components, the JangoMail service
can also handle unsubscribes and bounces automatically and synchronize these
with your original web database. The only setup that is required is the
placement of one ASP file on your web server. Other features include
message open tracking and click tracking.
|
[Top]
|
|
|
JMail
|
|
Send Email directly from you web page via your webserver. jMail will not start up any annoying email clients, just smoothly send the mail via the mailserver. Implement it with easy ASP code.
|
[Top]
|
|
|
|
Mail for .NET
|
|
Mail for .NET is the first product for the NetToolworks.NET framework. Together they provide methods that send, receive, compose, edit, encode and decode e-mail messages. SMTP, POP, complex MIME messages, HTML messages, and file/memory streaming are also supported.
|
[Top]
|
|
|
|
OCXMail
|
|
A single component that is limited in scope to five methods. The OCXMail ASP component allows you to send mail using the standard SMTP protocol from any program that can use ActiveX/OLE components.
|
[Top]
|
|
|
|
ocxQMail
|
|
The ocxQmail ASP component allows you to send mail using the standard SMTP protocol from any program that can use ActiveX/OLE components. ocxQmail queues up messages for batch delivery by a companion NT Service at intervals you specify in the Administration Windows GUI.
Your ASP pages do not have to wait for the mail message to be physically sent before continuing.
|
[Top]
|
|
|
|
RobustPop3
|
|
RobustPOP3 component allows you to retrieve mail using POP3 protocol. Features include: Retrieve Messages
Multiple File Attachments, File Attachments support MIME and UUEncode.
|
[Top]
|
|
|
|
SA-SmtpMail
|
|
A full-featured SMTP e-mail client component that allows developers to send e-mail from any client. This award-winning control offers significantly better performance than other popular SMTP components. SoftArtisans SMTPmail is written in high-performance C++ and supports all threading models, file attachments and multiple encoding schemes. New features in version 2.0 include login authentication and mass mail. The new version also supports PGP encryption.
|
[Top]
|
|
|
|
|
Mailing List
Want to receive email when the next article is published? Just Click Here to sign up.
|
|