Friday, October 13, 2006

Sending Email in ASP Using CDONTS

I've already writen an article about sending email in Windows 2003 using CDO; no I'll present a method for sending email using ASP from Windows 2000.

In OS years, Windows 2000 is pretty ancient. Never-the-less, there are still plenty of web site hosted on Windows 2000 servers. On such a system, if you want to send mail from a web site, you need to use the CDONTS object as opposed to the CDO.

Here's the quick and easy for sending a text message
Dim MailBody
Dim objMail

MailBody = ""
MailBody = "This is an email from CDONTS." & vbCrLf
MailBody = MailBody & "It is easy to send mail using CDONTS" & vbCrLf
MailBody = MailBody & "Give it a try!"

Set objMail = CreateObject("CDONTS.NewMail")
objMail.From= "somebody@somewhere.com"
objMail.To= "somebodyElse@somewhereElse.com"
objMail.Subject="Text Email from CDONTS"
objMail.Body= MailBody
objMail.Send
set objMail=nothing
vbCrLf means Carriage return Line feed; this is to mark the end of a line and star a new one. The vb in fron mean - you guessed it - VBScipt. Ther are a bunch of other vb character codes available as well.

You can also send an strHTML formatted email with CDONTS:
Dim objMail
Dim strHTML

Set objMail = CreateObject("CDONTS.NewMail")
strHTML = ""
strHTML = "<!DOCTYPE strHTML PUBLIC""-//IETF//DTD strHTML//EN"">"
strHTML = strHTML & "<HTML>"
strHTML = strHTML & "<head>"
strHTML = strHTML & "<title>Sending CDONTS HTML Email</title>"
strHTML = strHTML & "</head>"
strHTML = strHTML & "<body bgcolor=""FFFFFF"">"
strHTML = strHTML & "<p><font size =""3"" face=""Arial""><strong>"
strHTML = strHTML & "It's also easy to send an HTML formatted email.</strong><br>"
strHTML = strHTML & "You can use HTML tags right in your string.</p>"
strHTML = strHTML & "<p align = ""center"">Beautiful HTML emails are only a momnet away!</p>"
strHTML = strHTML & "</body>"
strHTML = strHTML & "</HTML>"

objMail.From= "somebody@somewhere.com"
objMail.To= "somebodyElse@somewhereElse.com"
objMail.Subject="HTML FOrmatted Email from CDONTS"
objMail.BodyFormat=0
objMail.MailFormat=0
objMail.Body=strHTML

objMail.Send
set objMail=nothing
There are a few things worth talking about here: Notice the double quotes inside the strHTML assignments. When assign attributes to tags, you have to use either:

Double Quotes: <font size=""3"">
Single Quotes: <font size='3'>
No Quotes: <font size=3>

If you use a single quote, the script will think that's where the string assignment ends and your script will break.

If you want to include an image, you can point to any image by fully qualifying the src tag: <img src=""http://www.ImageServer.com/MyImage.jpg"">

If you send an HTML formatted mail to a mail reader that doesn't support HTML (most do), it will come out with the tags included.

With CDONTS, you can also send attachments and Carbon Copies. To send an attachment, use the AttachFile method. This method has three parameters:

1. Source, type String or Istream object. This parameter is required, and must contain the full path and file name of the attachment. Only C/C++ and Java programs can use an Istream object.

2. FileName (optional). This provides a file name to appear in the attachment's placeholder in the message. If not specified, the file name from the Source parameter is used.

3. EncodingMethod (optional). Indicates the encoding of the attachment. There are two possible values: 0, meaning the attachment is in UUEncode format; and 1, indicating the attachment is in Base64 format. (Base64 is the encoding scheme defined by MIME; UUEncode is an older format that you should use if you suspect your recipient(s) may not have a MIME-compliant system.) The default value of this parameter depends upon the MailFormat property. If the MailFormat property is set to 1, the default value of EncodingMethod is 0. If the MailFormat property is set to 0, the default value of EncodingMethod is 1.

Example of a completed AttachFile with all parameters:
objMail.AttachFile Server.MapPath("/DirectoryOnYourSite/TheFileToAttach.txt"),"FileYouAskedFor.txt",1

Here's the whole she-bang. IN this example, I want the file to be sent with the same name it has on my server, and since I'm setting MailFormat to 0, I dont' need to set the EncodingMethod parameter:
Dim MailBody
Dim objMail

Set objMail = CreateObject("CDONTS.NewMail")
objMail.From= "somebody@somewhere.com"
objMail.To= "somebodyElse@somewhereElse.com"
objMail.Cc="OneMoreGuy@OneMorePlace.com"
objMail.Subject="CDONTS Email with CC and Attachment"
objMail.MailFormat=0

objMail.AttachFile Server.MapPath("/DirectoryOnYourSite/TheFileToAttach.txt")

MailBody = "CDONTS Mail with file attached" & vbCrLf
MailBody = MailBody & "The file must reside on your web server" & vbCrLf
MailBody = MailBody & "The File must be less than 2 megs in size" & vbCrLf
MailBody = MailBody & "If you need to send larger files, check with your hosting company" & vbCrLf

objMail.Body= MailBody
objMail.Send
set objMail=nothing
More stuff to jam into your brain:

BodyFormat sets the text format of the mail object. It has two possible values: 0, which indicates that the body of the message includes Hypertext Markup Language (HTML); or 1, which indicates that the body of the message is plain text. The default value is 1, so this property does not need to be set for plain text messages.

MailFormat sets the encoding for the mail object. It has two possible values: 0, which indicates that the object is to be in MIME (Multipurpose Internet Mail Extension) format; or 1, which indicates that the object is to be in uninterrupted plain text. This property is optional, and its default value is 1. This property determines the default value for the EncodingMethod parameter in the AttachFile method (to be discussed later). When sending an attachment, set the MailFormat property to 0.

Importance (optional) sets the importance associated with the mail object. The possible values are: 0, indicating low importance; 1, indicating normal importance (default); and 2, indicating high importance. Format: objMail.Importance=2

The most common use of sending mail from a web page is having someone fill out a contact or some other form. In that case, you would wnat o have them enter their email address and message, which you could then grab from the form submission and assign in your Mail object.

Now go forth and spam not!

1 comment:

Anonymous said...

I am very thankfull to you because of your code i am able to attach file with email.

Thanx
my email:rai_rai2002@yahoo.com