Home page
 
 Home 
 ASP 
 PHP 
 SQL 
 HTML 
 JavaScript 
 Search 
 Contact 
 
Search
or browse popular tags
ASP Tutorial
Subscription

Sign up for the free email newsletter for new tips, tutorials and more. Enter your email address below, and then click the button.

Privacy Policy

RSS Twitter

Sending email with ASP

Print

In this article we are going to demonstrate how to generate and send emails with ASP. We will use CDOSYS - Microsoft's improved interface for SMTP email which was introduced in Windows 2000.

Microsoft has discontinued the use of CDONTs on Windows 2000, Windows XP and Windows 2003. If you have used CDONTs in your ASP applications, you should update the code and use the new CDO technology.

Note that to send an email with ASP from a local machine you need the running IIS SMTP Virtual Server.

Letís learn how to send emails from the following examples:

Sending a text email

Sending email with CDOSYS is a simple task. First you create a reference to the CDO component. Then fill-in From, Subject and To fields of the headers and the body text. Also you can add Bcc and Cc headers. Then you use the Send method to send the email. Thatís all.

<%
Set Mail=CreateObject("CDO.Message")
Mail.Subject="Email subject"
Mail.From="from@domain.com"
Mail.To="to@domain.com"
Mail.Bcc="someone1@domain.com"
Mail.Cc=someone2@domain.com
Mail.ReplyTo ="replyto@domain.com"
Mail.TextBody="This is an email message."
Mail.Send
set Mail =nothing
%>

Back to top

Sending an HTML email

The process is quite similar to sending the regular text message.

<%
Set Mail=CreateObject("CDO.Message")
Mail.Subject="Email subject"
Mail.From="from@domain.com"
Mail.To=to@domain.com
Mail.ReplyTo ="replyto@domain.com"
Mail.HTMLBody = "<h1>This is an email message.</h1>"
Mail.Send
set Mail=nothing
%>

Back to top

Sending an email with an Attachment

Please note, when using the .AddAttachment method in your scripts you must use a fully qualified pathname as the argument to the method.  Using just a file name or a relative path will produce the error The specified protocol is unknown. By repeating the .AddAttachment method you can attach more than one file.

<%
Set Mail=CreateObject("CDO.Message")
Mail.Subject="Email subject"
Mail.From="from@domain.com"
Mail.To="to@domain.com"
Mail.TextBody="This is an email message."
Mail.AddAttachment "c:\mydocuments\test.txt"
Mail.Send
set Mail=nothing
%>

Back to top

Sending an email using a remote server

Sometimes you need to send an email using another server. To accomplish that you need to set the configuration properties to the server.

<%
Set Mail = CreateObject("CDO.Message")

'This section provides the configuration information for the remote SMTP server.

'Send the message using the network (SMTP over the network).
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="mail.yourdomain.com"
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

'Use SSL for the connection (True or False)
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False

Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

'If your server requires outgoing authentication, uncomment the lines below and use a valid email address and password.
'Basic (clear-text) authentication
'Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'Your UserID on the SMTP server
'Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="mail@yourdomain.com"
'Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="yourpassword"

Mail.Configuration.Fields.Update

'End of remote SMTP server configuration section

Mail.Subject="Email subject"
Mail.From="from@domain.com"
Mail.To="to@domain.com"
Mail.TextBody="This is an email message."

Mail.Send
Set Mail = Nothing
%>

Back to top

Sending an email via Gmail

<%
Set Mail = CreateObject("CDO.Message")

Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="smtp.gmail.com"
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465

Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1

Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="username@gmail.com"
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="yourpassword"

Mail.Configuration.Fields.Update

Mail.Subject="Email subject"
Mail.From="from@domain.com"
Mail.To="to@domain.com"
Mail.TextBody="This is an email message."

Mail.Send
Set Mail = Nothing
%>

Back to top

Load Recipients from a Database

This task may be accomplished in several ways. This is one of many methods.

For example, our database is an MS Access database stored on the local disk. We are interested in Customers table and Name and Email fields.

Set dbConnection = CreateObject("ADODB.Connection")
dbConnection.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=c:\Databases\Customers.mdb"
SQLQuery = "SELECT Name, Email FROM Customers"
Set Result = dbConnection.Execute(SQLQuery)
if Not Result.EOF then
  Do While Not Result.EOF
    SendMail Result("Name"), Result("Email")
    Result.MoveNext
  Loop
end if
dbConnection.Close

As you can see the code is simple. We create a database connection object then open the database and query it for the Name and Email fields of each customer. Those values are passed for each customer to the procedure that sends an email to the customer. Note that the recipient's address is formatted as "Name" <email@domain.com> for a more professional look to the recipient.

Sub SendMail(name, address)

Dim Message, SendTo
SendTo = Chr(34) & Name & Chr(34) & "<" & Address & ">"

Set Mail = CreateObject("CDO.Mail")
Mail.Subject = "Email subject"
Mail.From = "from@domain.com"
Mail.To = SendTo
Mail.TextBody="This is an email message."

Mail.Send
Set Mail = Nothing

End Sub

Back to top

Set the priority/importance of an email and request a read/return receipt

<%
Set Mail = CreateObject("CDO.Message")

' Set the priority/importance of an email
' For Outlook:
Mail.Fields.Item(cdoImportance) = cdoHigh 
Mail.Fields.Item(cdoPriority) = cdoPriorityUrgent 
' For Outlook Express:
Mail.Fields.Item("urn:schemas:mailheader:X-Priority") = 1

' Request a read/return receipt
' Read receipt
Mail.Fields("urn:schemas:mailheader:disposition-notification-to") = "admin@domain.com"
' Return receipt
Mail.Fields("urn:schemas:mailheader:return-receipt-to") = "admin@domain.com" 

Mail.DSNOptions = 14
Mail.Fields.update

Mail.Subject = "Email subject"
Mail.From = "from@domain.com"
Mail.To = "to@domain.com"
Mail.TextBody="This is an email message."

Mail.Send
Set Mail = Nothing
%>

Possible values of the email priority and importance:

CDO Constant Numeric Value Definition
cdoPriorityNonUrgent -1 Non-urgent priority
cdoPriorityNormal 0 Normal priority
cdoPriorityUrgent 1 Urgent priority
cdoLow 0 Low importance
cdoNormal 1 Normal importance
cdoHigh 2 High importance

Back to top

Related information




Tags: ASP SEND EMAIL TEXT HTML ATTACHMENT SMTP SERVER DATABASE PRIORITY IMPORTANCE READ RETURN RECEIPT

Add To: Add to dzone dzone | Digg this digg | Add to del.icio.us del.icio.us | Stumble it stumbleupon

  • Comments





Copyright © 2005-2014             www.WebCheatSheet.com All Rights Reserved.