|
This article assumes a basic knowledge of HTML and Active Server Pages
development.
CDONTS is a subset of the features found in the Microsoft Collaboration
Data Object which is used to programmatically manipulate Exchange Server.
CDONTS is limited to some simple message services and is available on all
versions of Windows 2000 and Windows NT 4 Server with Service Pack 4.
Additionally, CDONTS requires that you have installed IIS and its SMTP
component.
I will provide a brief description of this handy service and let you find
your own applications for it. There are many possible uses. You may want
to use CDONTS to send form results to your users; to allow a user to send
web page content to a third person in either HTML or plain text format or
to send an email from your website with file attachments. The uses are
limited by your imagination but the methods used to implement this feature
are really quite basic.
First lets be clear that CDONTS is run by the server and not as a client
script. Many of the difficulties I’ve seen with first-time users is that
they try to script CDONTS from the web page sent to the client browser.
CDONTS is ALWAYS run on the server and coded on the server in your Active
Server Page.
In this article I will show you how to send email using a simple web form
and the CDONTS NewMail object.
First you will need to create an instance of CDONTS with the following
line of code:
Set objMail = Server.CreateObject("CDONTS.NewMail")
Couldn’t be simpler, and the rest is just as easy! There are only eight
simple Properties to deal with and one Method.
Here’s a list of the NewMail Properties:
To
Cc
Bcc
From
Subject
Body
AttachFile
BodyFormat
MailFormat
The one Method is:
Send
The ‘To’, ‘Cc’, and ‘Bcc’ properties contain the recipient
addressing information of the email. Each email must contain at least one
of the ‘To’, ‘Cc’ or ‘Bcc’ address components filled in. They
represent, in order, the To, Carbon Copy, and Blind Carbon Copy fields of
the email.
The address form for these properties is the familiar user@host
For example:
me@myhost.com
Paul R. Sadowsk<sadowski@parkedge.com>
Each of these properties accepts multiple addresses as a comma separated
list.
The ‘From’ property should be set to the mail address of the sender.
This can be your email address, your user’s email address or an
information account on your server such as postmaster.
The address form is the same as above:
user@host or PersonalName<user@host>
The ‘Subject’ property is the text of the email’s subject line:
For example:
Subject = "Test Email"
The ‘Body’ property is the text that makes of the body of the email.
For example:
BodyText = "This is a test."
The ‘AttachFile’ property is used when sending a file as an attachment
in MIME format with Base64 encoding. The value of this property is a
complete pathname to the file you want sent as an attachment.
For example:
AttachFile="c:\stuff\mydata.doc"
The ‘BodyFormat’ and ‘MailFormat’ properties set whether the message is sent as plain
text or as HTML email. The possible values for this property are:
0 : HTML Email
1 : Plain Text
By default these properties have a value of 1.
The ‘Send’ method is invoked with no arguments and causes the message
to be sent to your SMTP service for delivery.
Let’s put it all together now into a sample email:
Set objMail = Server.CreateObject("CDONTS.NewMail")
objMail.To = “Some Guy<someguy@guy.com>”
objMail.From = “Paul<me@my.com>”
objMail.Subject = “Sample Email Sent with CDONTS”
BodyText = “This is a sample email.” & vbCRLF
BodyText = BodyText & “It was sent using CDONTS on Windows
2000!” & vbCRLF
BodyText = BodyText & vbCRLF & “Paul”
objMail.Body = BodyText
objMail.Send
It’s really just that simple. If the BodyText variable contained HTML
then just add these lines before the ‘Send’ method:
objMail.BodyFormat = 0
objMail.MailFormat = 0
To send an attached file, add this line before the “Send” method:
objMail.AttachFile “c:\somedir\somefile.ext”
That’s all there is to sending email with CDONTS. I’ve saved the
example that uses a web based form until the end because there are a few
things you’ll probably want to consider when allowing users to send
email this way.
First, in order to avoid abuse of your email system you may want to limit
who the form based email can be sent to. You can script to check the
domain portion of the To, Cc and Bcc fields and require that they match
your domain or other domains that you control.
You may elect under certain circumstances to NOT allow the user to input
recipient addresses and use fixed addresses in the form… customer
service, sales and information requests are good examples where you might
do this. You can fix the possible recipients and let the sender select one
or more from a dropdown listbox.
You should always parse the addresses to see that they appear to have a
valid format, and you should always either log or add to the email body
text the sending IP of the user using ASP’s Request.ServerVariables.
You should code your ASP page to accept forms only from a specific
referrer or referrers on your host only and limit the size of the email
body.
Having said all that, here’s a simple form and it’s associated ASP
page to send the email:
==sendmail.htm==
<html>
<head>
<title>Send Mail</title>
</head>
<body bgcolor="#FFFFFF">
<form method="POST" action=”/scripts/sendmail.asp”>
<h3 align="center">CDONTS Send Email Simple Form
Example</h3>
<p>To: <input type="text" size="30"
name="To"></p>
<p>From: <input type="text" size="30"
name="From"></p>
<p>Subject: <input type="text"
size="30" name="Sub"></p>
<p>Message Text:<br>
<textarea name="Message" rows="10"
cols="80"></textarea></p>
<p align="center"><input type="submit"
name="Send"
value="Submit"><input type="reset"
name="Cancel"
value="Reset"></p>
</form>
</body>
</html>
==end sendmail.htm==
==sendmail.asp==
<%@ LANGUAGE="VBSCRIPT" %>
<%
Dim objMail, strFrom, strTo, strSubject, strBody
strTo = Trim(Request.Form("To"))
strSubject = Trim(Request.Form("Sub"))
strFrom = Trim(Request.Form("From"))
strBody = Trim(Request.Form("Message"))
Set objMail =
Server.CreateObject("CDONTS.NewMail")
objMail.From = strFrom
objMail.To = strTo
objMail.Subject = strSubject
objMail.Body = strBody
objMail.Send
Set objMail = Nothing
%>
==end sendmail.asp==
You can learn more about CDONTS at http://msdn.microsoft.com
© 2000 by Paul R. Sadowski
All Rights Reserved. Used By Permission.
Comments to: aa089@bfn.org
|