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"
.Fields.Update
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 = "[email protected]"
.To = "[email protected]; [email protected]"
.Cc = "[email protected]"
.Bcc = "[email protected]"
end with
%>
It is time to set the subject and body text. To send a 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
msg.Send
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.
Item | Example | Description |
---|---|---|
smtpserverport | 25 | Default 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. |
smtpusessl | True | Default is False . Set to True if SMTP requires SSL. |
smtpconnectiontimeout | 60 | Default is 30 seconds. |