|
Sending
mails in ASP using CDONTS
This article explains how to send
emails from your website using ASP. All you need is to have the IIS with CDONTS
components (which is standard with IIS5) installed and the SMTP virtual
server running on your web server. Any web hosting service with ASP should
already have this already installed on the server.
CDONTS cannot be installed on
Win9x systems. On NT4 and Windows 2000, the CDONTS component is installed when you install IIS. Although
CDONTS will run on Windows XP, Microsoft has decided to remove the component from IIS 5.1 on Windows XP, so
if your server is on Windows XP then you will have to get a copy of cdonts.dll and register it on
your IIS web server.
To send mails using CDONTS, you
need to have the SMTP server that ships with IIS 4 or 5 installed and
running on your webserver. The SMTP server is usually installed by default
during the standard IIS installation.
Now I shall assume that you have
the minimum required knowledge of writing ASP scripts. If you know ASP,
then sending mails
using CDONTS component is fairly simple and straightforward.
Sending Text
based email
<%
Dim objMail 'This holds the CDONTS NewMail
object
Set objMail = CreateObject("CDONTS.NewMail") 'Creates
an instance of the CDONTS.Newmail on the server
objMail.From= "somebody@thisuniverse.com" 'Set
the mail ID from which the mail is being sent
objMail.To= "nobody@thatuniverse.com" 'Set the
mail ID to which the mail is being sent
objMail.Cc= "man1@hitxp.com;man2@hitxp.com" 'Carbon
Copy to be sent if any
objMail.Bcc= "nobody3@thatuniverse.com" 'Blind
Carbon Copy to be sent if any
objMail.Subject="Type the subject here"
'Set the mail Subject of the mail
objMail.Body= "This will be the message body"
'Set
the Body of the message
objMail.Send 'Send the email
Set objMail=nothing 'Free up the server
resources
%>
Sending
HTML based email
By default, CDONTS sends text
based emails. If you want to send HTML emails, then all you have to do is
add the following 2 extra lines in your code.
objMail.BodyFormat=0
objMail.MailFormat=0
In this case also do not forget
to write the body text in the HTML format using HTML tags.For Ex:
objMail.Body=0"<HTML><HEAD><TITLE>ASP HTML Sample</TITLE></HEAD><BODY bgcolor='#000000'><OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' WIDTH='230' HEIGHT='30' id='pl1' ALIGN=''> <PARAM NAME=movie VALUE='pl1.swf'> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#000000> <EMBED src='pl1.swf' quality=high bgcolor=#000000 WIDTH='230' HEIGHT='30' NAME='pl1'></EMBED></OBJECT></BODY></HTML>"
Sending
attachments with email
To send attachments along with
your email, you have to just add the following line in the ASP code.
objMail.AttachFile Server.MapPath("/somefolder/file1.zip")
or if you know the absolute path
objMail.AttachFile("C:\somefolder\file1.zip")
NOTE: You must have the files to
be attached to be present on your server (or be accessible on a remote
machine by the IUSR_MachineName account) and the IUSR_MachineName
must have the Read permission on the File to be attached. To allow
any visitor to your page to attach a file to the mail, you'll have to
first allow the client machine to upload that file to your server and then
attach the file to the mail. But uploading of files can be dangerous and
pose a security risk to your server as it would allow users to write files
on to your server.
If you try to attach a file that
does not exist then you'll receive an Unspecified Error.
NOTE:
-
As it is quite obvious, CDONT
scripts can be used to send unauthenticated emails. So be careful
while allowing users to send mails using this option.
-
Once a mail is sent using
a CDONTS object you cannot use it again to send another mail.
For that you'll have to release all its resources by setting it to Nothing
and then create a new CDONTS object using CreateObject("CDONTS.NewMail")
and then use it send another mail.
A
Brief Description of CDONTS Properties and Methods
| Property |
Description |
| Bcc |
Sends a blind copy of the
message to each of the mail IDs in this semicolon separated list |
| Body |
contains the content of
the message in HTML or Text Format |
| BodyFormat |
sets the text format of
the message body |
| Cc |
Sends a copy of the
message to each of the mail IDs in this semicolon separated list |
| ContentBase |
Sets the base property
for the URLs in the message body |
| ContentLocation |
Sets the absolute or
relative paths for the URLs in the message body |
| From |
mail ID of the sender |
| Importance |
Priority flag of the
message: 0=CdoLow, 1=CdoNormal,2=CdoHigh |
| MailFormat |
Sets the Mail Encoding
Format to Text or MIME |
| Subject |
Subject of the message |
| To |
mail ID of the receiver |
| Value |
Additional message header
information |
| Version |
Gets the version of the
cdonts.dll file |
| Method |
Description |
| AttachFile(FilePath,[name],[encoding]) |
Adds and attachment to
the Current Mail. The file must be local to the server. |
| AttachURL |
Adds a URL as an
attachment to the message |
| Send |
Sends the message to the
recipients mentioned in the To, Cc, Bcc fields. |
| SetLocaleIDs |
Sets the user's Local
Messaging Environment variables |
-by Gurudev
MADE IN
INDIA
gurudevp@vsnl.net
On 13 November 2002
Home
>> Computers >> Programming
>> ASP
|