How do I send email from an ASP page using ASPMail?

About AspMail

AspMail allows you to send mail using the standard SMTP protocol from any program that can use ActiveX/OLE components. Features include:

  1. SMTP (sending) Messages
  2. Multiple File Attachments
  3. File attachments support MIME and UUEncoding
  4. US Ascii and ISO-8859-1 character sets
  5. PGP
  6. Subject line encoding for 8bit message subjects
  7. Redundant SMTP servers (If the primary SMTP server is down, the secondary server is used)
  8. Special Header Support (Standard X-Priority headers, MS Mail (including Exchange) priority headers, Urgent header, ConfirmReading and ReturnReceipt Headers)
  9. Multiple concurrent users (Tested with 15 concurrent connections)

Simple Mail Example

Using the component is as simple as

  1. Creating the object
  2. Setting a few properties
  3. Calling the SendMail method

The following code demonstrates how to use AspMail from VBScript. In this example Joe from Joe’s Widgets wishes to send an email to John Smith. Joe’s mail server is located at mailhost.localisp.net.

Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.FromName   = "Joe’s Widgets Corp."
Mailer.FromAddress= "Joe@somehost.com"
Mailer.RemoteHost = "mailhost.localisp.net"
Mailer.AddRecipient "John Smith", "jsmith@anotherhostname.com"
Mailer.Subject    = "Great SMTP Product!"
Mailer.BodyText   = "Dear Stephen" & VbCrLf & "Your widgets order has been processed!"
if Mailer.SendMail then
  Response.Write "Mail sent..."
else
  Response.Write "Mail send failure. Error was " & Mailer.Response
end if

By testing the result of the SendMail method we can determine if the mailing process was successful or not.

Form Handling

All or partial input for a message may come from a form. For example, a form posted to the server with a request method of GET (i.e. form action="/scripts/AspMail.asp" method=get ) may provide the message recipient’s email address, subject and message text as follows:

Mailer.AddRecipient Request.QueryString("ToName"), Request.QueryString "ToAddress")
Mailer.Subject   =  Request.QueryString("Subject")
Mailer.BodyText  = Request.QueryString("MsgBody")

The form may also use the POST method (i.e. form action="/scripts/AspMail.asp" method=post ) in which case the code would look as follows:

Mailer.AddRecipient Request.Form("ToName"), Request.Form("ToAddress")
Mailer.Subject   =  Request.Form ("Subject")
Mailer.BodyText  = Request.Form ("MsgBody")

You can use any mixture of static and dynamic data in setting the components properties as dictated by your needs. For example, you may wish to send the mail to a single user. In this case you could modify the code to look something like this:

Mailer.AddRecipient "John Smith", "jsmith@alocalhost.com"
Mailer.Subject   =  Request.QueryString("Subject")
Mailer.BodyText  = Request.QueryString("MsgBody")

Generic Form Handling

In some cases users may wish to use a number of different forms to send email with the same block of code. ASP allows you to loop through each QueryString or Form variable and append each one to string variable which is then assigned to the BodyText property. Please note: AspMail cannot control the order that these variables are returned in. This is a function of ASP, not AspMail. ASP takes the form variables and creates the appropriate Request collection (QueryString or Form) and stores the data in an order that AspMail cannot change. If you use this method you must accept ASP's order.

strMsgHeader = "Form information follows" & vbCrLf
for each qryItem in Request.QueryString
strMsgInfo = strMsgInfo &  qryItem & " - " & request.querystring(qryItem) & vbCrLf
next
strMsgFooter = vbCrLf & "End of form information"
Mailer.BodyText = strMsgHeader & strMsgInfo & strMsgFooter

Setting Mail Priority

There are a couple of headers that can be modified to set message priority.

The Priority property sets the message priority on a scale of 1 to 5. A priority of 1 means HIGH. A priority of 3 means NORMAL and a priority of 5 means LOW. In addition to this you can also set the Urgent property if the message status is urgent. The Urgent property is a true/false property.

How to Use the DateTime Property

The component creates a Date/Time value for the message based on the calculated GMT time. The DateTime property was added to allow users to set a custom date/time timezone. The following code demonstrates how to set the DateTime to US Central Standard Time. By slightly altering the code you can adjust this to work for your own timezone.

function DayName (intDay)
  select case intDay
    case 1
      DayName = "Sun"
    case 2 
      DayName = "Mon"
    case 3 
      DayName = "Tue"
    case 4 
      DayName = "Wed"
    case 5 
      DayName = "Thu"
    case 6 
      DayName = "Fri"
    case 7 
    DayName = "Sat"
  end select
end function

function MonthName (intMonth)
  select case intMonth
    case 1
      MonthName = "Jan"
    case 2
      MonthName = "Feb"
    case 3
      MonthName = "Mar"
    case 4
      MonthName = "Apr"
    case 5
      MonthName = "May"
    case 6
      MonthName = "Jun"
    case 7
      MonthName = "Jul"
    case 8
      MonthName = "Aug"
    case 9
      MonthName = "Sep"
    case 10
      MonthName = "Oct"
    case 11
      MonthName = "Nov"
    case 12
      MonthName = "Dec"
  end select
end function

[set other Mailer properties]
Mailer.DateTime = DayName (WeekDay(Date)) & ", " & Day(Date) & " " & MonthName(Month(Date)) & " " & Year(Date) & " " & FormatDateTime(Now, 4) & " -0600 (CST)"
Mailer.SendMail

Notes About Creating the Mailer Object

You can create the mailer object at two different points in time:

  • Immediately before sending an email
  • At the session scope and saved as a session object

You will have to decide when and where it is appropriate to create the object based on your particular application. If you aren't sure which way to create the object reference, or for typical usage, you should create the object immediately before sending your email. Your code would look like this:

Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
... [Set properties]
if Mailer.SendMail then ...

Creating these local references, as demonstrated above, allow you to use the object on multiple application threads at the same time.

To create an object reference at the session level, your code might look something like this:

if Not IsObject (session("Mailer")) then
  Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
  Set session("Mailer") = Mailer
else
  Response.write "Cached session object reference being used

" Set Mailer = session("Mailer") end if

Multiple Host Support

AspMail provides one host property to set up remote SMTP server addresses. The RemoteHost property should be set to your primary and secondary server’s address seperated by semicolons. In the event that the primaryserver is down, AspMail will attempt to use the secondary server. For example,

Mailer.RemoteHost = "mailhost.localisp.com;mailhost.anotherisp.com"

Questions about AspMail

See http://www.serverobjects.com for more detail on troubleshooting and component properties.

  • 33 Users Found This Useful
Was this answer helpful?

Related Articles

Where can I find documentation for ServerObject's AspMail component?

This documentation can be found at: http://www.serverobjects.com/products.html For...

How do I upload files through my web pages?

You can use Software Artisan's SAFileUP. This is an ASP component that allows you to upload files...

What is PageCounter and how do I use it?

About Page Counter PageCounter is an object that counts and displays the number of times a Web...

How do I setup Microsoft Outlook Express to read my e-mail?

Follow these steps: Open Microsoft Outlook Express. From the Tools menu, click...

How do I setup Netscape Messenger to read my e-mail?

Follow these steps: Open Netscape Navigator. Click the Edit menu and then click Preferences......