ASP MAIL - Sample and Documentation
Simple Mail Example
Using the component is as simple as
- Creating the object
- Setting a few properties
- Calling the SendMail method
The following code demonstrates how to use AspMail from VBScript. In
this example Joe from Joes Widgets wishes to send an email to John Smith. Joes
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 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
recipients 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.
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
To return form contents in the original form order
your code might be...
strMsgHeader = "Form Information Follows: " & vbCrLf
for i = 1 to Request.Form.Count
strMsgInfo = strMsgInfo & Request.Form.Key(i) & " - " & Request.Form.Item(i) & 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.
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<p>"
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
servers address separated by semicolons. In the event that the primary server
is down, AspMail will attempt to use the secondary server. For example,
Mailer.RemoteHost = "mailhost.localisp.com;mailhost.anotherisp.com"
|