When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
spgif spgif
ASP ASP.NET ASP FAQs Feedback topnav-right
Print this page.
Published: Sunday, November 22, 1998

Sending Emails in ASP using CDO

For More Information on Sending Email via ASP Pages...
For more information on sending emails through an ASP page, be sure to check out the Email FAQ Category at ASPFAQs.com. There you will find FAQs describing how to receive email from POP accounts, how to validate user-entered email addresses, how to send HTML-formatted emails, etc.

- continued -

Sending email through Active Server Pages is not a difficult task. All you need is love. No, wait; all you need is the Collaborative Data Object, which ships with NT Option Pack 4. To make sure you have it installed, go to Start / Control Panel / Add/Remove Programs / NT Option Pack 4, and see if the SMTP Piece has been installed. If it is *not* installed, you will get an error when you try to run some of the code shown below (the error will read something like, "Invalid class string").

To create an instance of a CDO object in your ASP code, it is as simple as:


Dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail")

Now you're ready to send those emails off! CDONTS.NewMail has a few obvious, easy to use properties and methods. Let's look at some code:


' This code assumes the above CDO
'instantiation code is included
objCDO.To = "mitchell@4guysfromrolla.com"
objCDO.From = "gates@microsoft.com"
objCDO.cc = "barksdale@netscape.com,jobs@apple.com"

Dim txtSubject
txtSubject = "Hello Scott! We were wanting your advice on some programming issues. Please come to Redmond at your earliest convenience for a very fat check!"

objCDO.Subject = "Attn: Scott, we need you!!"
objCDO.Body = txtSubject

That's all there is to it. The properties are hopefully fairly obvious. In the above code an email is being sent to mitchelL@4guysfromrolla.com from gates@microsoft.com, and being CC'ed to barksdale@netscape.com and jobs@apple.com. The subject is simply the email message. The send method is what officially dispatches the email (don't forget this, or your email won't go anywhere!!)

CDO is fairly powerful (and bloated). You can send attachments, send to group lists, send schedules (like in Outlook).

I also have written an article which was published on 15Seconds.com dealing with uses of the CDO object. In the article (available through this URL) I discuss marketing through personalized emailing. The article outlines a good use for the CDO object.

Let's examine some of the other features of CDO. Below is a snippet of code which displays some of CDO's extraneous features:


objCDO.To = "someone@xyz.com (John Doe)"
objCDO.From = "me@abc.com (Jane Doe)"
objCDO.bcc = "janedoe@aol.com" 'Blind cc
objCDO.Subject = "My Resume, per Request"
objCDO.Body = "Hello John. Here is a copy of my resume"
objCDO.Importance = 2 'High importance!
objCDO.AttachFile "\\server\jane\resume.doc","Resume.doc"
objCDO.Send 'Send off the email!

Set objCDO = Nothing

[View a live demo]

If the file specified by the AttachFile method does not exist, you will receive an Unspecified Error error message. For more information on sending attachments be sure to read: How do you send email attachments via an ASP page?

As you can see, I've introduced two new properties and a new method. Bcc is for blind carbon copying. If you send an email "to" someone, and blind carbon copy someone, the person who receives the email doesn't know that the message was carbon copied to those on the bcc list. I also used the Importance property, which takes three values:

    0 - Low
    1 - Normal
    2 - High

It defaults to 1, Normal, if not explicitly set. You'll also note that for the email addresses I wrote something to the effect of: "emailaddress@something.com (Name)" Whatever you put in parenthesis after the email address will appear as the name of the person who sent the email. So rather than seeing the email message from me@abc.com, John Doe will see that it was from Jane Doe.

The new method introduces is the AttachFile method. This method, as its name suggests, attaches a file to the email being sent. It takes up to three parameters, but let's just discuss the first two. The first parameter is the file you want to attach. In this example I attached \\server\jane\resume.doc. The second parameter is optional, and it specifies what you want the name to be in the email message. Here you can pick some prettier name (like "Jane's Nifty Resume" if you like) than the full file name. I just named it "Resume.doc".

Getting a Permission Denied Error?
Depending on the security settings on the Web server you may receive a permission denied error when executing the Send method. If this is the case check to make sure that the IUSR_MachineName has Full Control permissions on the mail root directories (usually C:\InetPub\mailroot\). (For example, if your Web server's name is Bob, ensure that IUSR_Bob has these permissions.)

Happy Programming!

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article