Did you find this useful?
Socialize it today.

Sending Email Using CDOSYS in ASP

Sunday, September 09, 2012

tags ASP

Sending email from an ASP web application is a fairly easy task. If you are hosting your ASP web application on a server running Windows 2000 or later, you'll find that using CDOSYS (Collaboration Data Objects) is your best bet for sending email from ASP pages. The following ASP example can be used to send email from an ASP page.

We can start building the ASP page by creating a Message and Configuration object.

Set msg = Server.CreateObject("CDO.Message")
Set conf= Server.CreateObject("CDO.Configuration")

Next, set the configuration object up as shown below. You will need to provide the correct value for the smtpserver item. These two items are generally the minimum information that is required. However, depending on the configuration of your SMTP server, you have to provide a specific port, enable SSL, or authenticate. At the end of this tutorial, some additional configuration items have been listed.

confURL = "http://schemas.microsoft.com/cdo/configuration/"
with conf
    .Fields.Item(confURL & "sendusing") = 2
    .Fields.Item(confURL & "smtpserver") = "smtp.mailserver.com"
end with

The following step is to specify the recipients. You can send to multiple recipients by separating addresses with a semicolon, as shown with the To property below

with msg
    .From = "you@yourdomain.com"
    .To = "someone@domain.com; somebody@domain.com"
    .Cc = "someoneelse@domain.com"
    .Bcc = "someone@anotherdomain.com"
end with

It is time to set the subject and body text. To send plain text email, use the TextBody method. To send HTML email, use the HTMLBody method.

with msg
    .Subject = "My message subject"
    .TextBody = "This is a plain text email"
    .HTMLBody = "<em>This is an HTML email</b>"
end with

The final step is to bind the configuration to the CDO Message and send the email message.

msg.Configuration = conf
Set conf = Nothing
Set msg = Nothing

Here are some additional useful optional Field Items that you may need to use if your SMTP server requires a different port, a secure channel, and/or authentication.

smtpserverport25Default is 25 but your SMTP server may be using an alternate port.
smtpauthenticate"1"If your SMTP server requires authentication, set this value.
sendusername"username"A username that has permission to send.
sendpassword"password"A password associated with the username.
smtpusesslTrueDefault is False. Set to true if SMTP requires SSL.
smtpconnectiontimeout60Default is 30 seconds.

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.

Recommended Books & Training Resources

Beginning ASP Databases Sams Teach Yourself Active Server Pages 3.0 in 21 Days ASP In A Nutshell