Sending Email With ASP
By Stephen Bucaro
Sending email with ASP (Active Server Pages) is not difficult. You can use ASP
email to create a contact form, support request form, or even perform mass emailings.
CDO (Collaboration Data Objects) is the Microsoft technology that simplifies the
creation of email applications.
SMTP (Simple Mail Transfer Protocol) is a service installed with IIS that allows
you to send email from an ASP webpage. This article assumes SMTP is properly
installed and configured on your server.
previous technology to send email was CDONTS (Collaboration Data Objects for New Technology
Server). With Windows 2000 server, CDONTS was replaced with CDO. CDO which provides
the ability to send messages through a remote SMTP (Simple Mail Transfer Protocol) server.
Windows 2003 server doesn't install CDONTS by default, therefore if you used CDONTS
in your ASP applications, you should update the code to use CDO. You can use CDO
(also referred to as CDOSYS) in ASP and ASP.NET applications.
Sending a Plain Text Email
The script below demonstrates how to send a simple pre-defined plain text email message.
Set objMail. = CreateObject("CDO.Message")
objMail.Subject = "Sending email with CDO"
objMail.From = "firstname.lastname@example.org"
myMail.To = "email@example.com"
objMail.TextBody = "This is a test message."
set objMail. = Nothing
Response.Write "Message Sent"
To try the example, paste the script into a file on your ASP Web server named, for
example cdotest.asp. Edit the From and To email addresses, then
execute the script.
An ASP Email Contact Form
One of the most common uses of ASP email is to create a Contact Form for your Web site
through which visitors can provide comments or ask questions. Below is an example of
an ASP Web page for a Contact form.
<title>Email Contact Form</title>
Dim strName, strEmail, strMessage
strName = Trim(Request.Form("sender"))
strEmail = Trim(Request.Form("email"))
strMessage = Trim(Request.Form("comment"))
If(strMessage <> "") Then
Set objMail = CreateObject("CDO.Message")
objMail.From = strEmail
objMail.To = "firstname.lastname@example.org"
objMail.Subject = "Contact Form from " & strName
objMail.TextBody = strMessage
Set objMail = Nothing
Response.Write "Thank You"
Email Form<br />
Name: <input type="text" name="sender" size=40></input><br />
Email: <input type="text" name="email" size=40></input><br />
<textarea name="comment" cols=40 rows=5></textarea><br />
<input type="submit" value="Submit"></input>
<% End If %>
Paste this script into a file on your Web server named, for example contact.asp.
Edit the To email address to the address where you want to receive the contact form
messages. You can also edit the text "Thank You" which is the message the user receives
after they click the [Submit] button.