Sending Email

Sending Email from Web
Sites hosted by Elf may send standard SMTP email from web pages using an ASP component called AspQMail from Server Objects. Directions for using this component are given below. These are adapted from the developer's website, where further information on this and other useful asp components can be found.

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

Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.QMessage = true
Mailer.FromName = "Joe’s Widgets Corp."
Mailer.FromAddress= ""
Mailer.RemoteHost = ""
Mailer.AddRecipient "John Smith", ""
Mailer.Subject = "Great SMTP Product!"
Mailer.BodyText = "Dear Stephen" & VbCrLf & "Your widgets order has been processed!"
if Mailer.SendMail then
  Response.Write "Mail sent..."
  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.

top of page

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", ""
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
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
strMsgFooter = vbCrLf & "End of form information"
Mailer.BodyText = strMsgHeader & strMsgInfo & strMsgFooter

' Note this code only works for forms containing 128 or fewer field items

top of page

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 values underlined you can adjust this to work for your timezone.

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

Common Questions and Problems

How do I determine a cause of mail failure?


The message is sent but I never receive it. What's wrong?


It's been working fine for 3 years and now it appears that the messages are not getting delivered.

Note about FromAddress: You must specify a value for the FromAddress property. Mail failure will occur without a FromAddress.

If the component can determine why the SendMail method failed, that information will be stored in the Response property. So, for example, to print that information to the clients browser you could add the following code:

if not Mailer.SendMail then if Mailer.Response <> ''" then strError = Mailer.Response else strError = "Unknown" end if Response.Write "Mail failure occured. Reason: " & strError end if

Another fairly common problem is when a user reports that a specific feature is not working. For example BCC's may seem to never reach their destination. A valuable debugging tool is available with the SMTPLog feature. Assign a valid filename to this property and the contents of the SMTP transaction that occurs during a SendMail call will be recorded to this file.

If you find that the SMTP transaction occurs without error then you should check elsewhere for the cause of mail delivery failure. Invariably the user finds that the BCC address was invalid or that the SMTP server is not relaying mail for the recipient domain. 

The SMTPLog feature allows you to verify if the transactions are complete and valid before pursuing other avenues in determining the cause of failure. If you see no errors in the SMTPLog then you need to check downstream of AspMail starting with the SMTP server you are using to send, as well as the recipient SMTP server. Anti-spam features on either machine may be identifying your message as spam and may need to be reconfigured. Check with the SMTP server administrators.

500 (503, 554 etc...) series errors are errors that the SMTP server generates. The exact meaning of the error depends upon your configuration and SMTP server. Please consult your SMTP server documentation for specifics on what these errors mean. Some of these are covered below but these errors are being issued by your SMTP server and AspMail is merely passing them on to you.

What would cause an "Operation timed out error"?


"AspMail has been working great but suddenly I'm getting an 'Operation timed out' error



Reasons for operation timed out include:

  1. SMTP server is down, overloaded or simply not responding
  2. Firewall blocking port 25 between AspMail and SMTP server
  3. Packet filtering blocking port 25 between AspMail and SMTP server
  4. IP route is down
  5. Your Winsock configured DNS server is down

To check using another method run telnet [smtp hostname] 25 from the IIS server where AspMail is installed. If telnet can see the SMTP server, then AspMail can see the server also.

Is there a comprehensive list of errors that can be returned by AspMail? There are quite a few errors that can be returned. We do not have a comprehensive list, primarily because these errors are returned from systems AspMail is dependent on. The errors can come from your SMTP server (errors vary from vendor to vendor), from WinSock errors (documented by Microsoft) and general OS errors such as out of memory, disk read/write failures etc... (again documented by Microsoft).
"AddCC or AddBCC doesn't work" - is this a bug with AspMail?


"I'm adding multiple recipients but only the first one is getting delivered. What's wrong with AspMail?"

AddRecipient, AddCC and AddBCC work just as they should. The problem is not with AspMail. It is likely that your SMTP server is rejecting "foreign addresses" (see "no relay" question below).

To test whether AspMail is functioning properly use the SMTPLog property to capture an SMTP session with multiple recipients. All the recipients you send to should appear in the log as the SMTP envelope is sent. If they all appear then the problem is with your SMTP server or an SMTP server down the stream (or your addresses are invalid).

The syntax
Mailer.AddRecipient = "name", ""
Mailer.AddRecipient =  ""
both return a VBScript error. What's wrong?
First off, remove the = sign. AddRecipient sounds like and is actually a method, not a property. Also AddRecipient takes two parameters. See AddRecipient in the methods listing for details.
What's the syntax for adding multiple recipients? Call AddRecipient, AddCC or AddBCC for each recipient. There's no other way to add multiple recipients.

Mailer.AddRecipient "name1", "address1"
Mailer.AddRecipient "name2", "address2"
Mailer.AddBCC "name3", "address3"

How do I create a hyperlink in my message?





top of page

Set Mailer.ContentType = "text/html" and include the hyperlink just like you would with html.

<head><title>AspMail HyperLink</title><head>
<H3>AspMail HyperLink</H3>
Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.FromName = "Status Update Report"
Mailer.FromAddress = ""
Mailer.RemoteHost = "your.smtp.server"
Mailer.AddRecipient "Your Recipient", ""
Mailer.Subject = "AspMail Hyperlink"
Mailer.ContentType = "text/html"
Mailer.BodyText = "<html><body>Here's some <a href="""">info</a></body></html>"
if Mailer.SendMail then
  Response.Write "Mail sent..."
  Response.Write "Mail failure. Check mail host server name and tcp/ip connection..."
  Response.Write "<p>" & Mailer.Response
end if
How do I create a line-break in a message? Under VBScript you can use the predefined constant VBCrLf. Simply using a Chr(13) or a Chr(10) will not work --you must use both -- the VBCrLf defined constant is the preferred method. A Carriage-return and line-feed character are required to create a new line in the message. See the sample scripts for examples.
My mail to AOL is bouncing. What am I doing wrong? AOL will not accept anything other than a true address in the "From:" heading. Just about every mail system out there, except AOL and Compuserve, will accept the "No Mail Address" as the "From:" header.
Can I repeatedly assign values to the BodyText property? and "The message text keeps growing with each email I send." Yes, the text will be appended to the message. Use ClearBodyText if you need to clear the message text.
How many recipients can I add to a message? To Recipients: 65K; CC's: 65K; BCC's: 65K. But many SMTP servers won't handle that many so we suggest you reduce the # per message sent.
The SMTP server is reporting back a "no relay" error. What's wrong?
also "foreign traffic not accepted" and other similar messages...
The SMTP server is looking at your FromAddress and determining that it doesn't know who you are. Some SMTP servers are configured to disallow the "relaying" or transfer of mail originating from addresses outside of its own domain. The only solution is to provide a FromAddress that's local to the SMTP server's domain or get the operator of the SMTP server to allow the FromAddress you are using. This setting is commonly used by ISP's to prevent spammers from using their resources.
What does the error "socket not connected" mean? This error occurs when the remote SMTP server abnormally closes the Winsock connection that AspMail established. The reasons may include an SMTP server that is not functioning properly (misconfigured or failing). In some cases this error can be generated by a Winsock layer that is failing. To test your local server, reboot the server and try to run the script again. If the error continues to occur it is likely that some sort of configuration problem or failure is occurring on the SMTP server.
What does error "554 No valid recipients" mean?


"503 Need RCPT (recipient)"

AspMail can encode high characters using a scheme where the = sign indicates a character to be decoded follow by the hex string value of the character to be encoded. This system of course assumes that the client can decode these characters (which most can). This is called quoted-printable encoding. The default for AspMail is not to use QP encoding. Things that trigger automatic QP encoding:

  1. High characters - characters with the following ordinal values 0..31,61,128..255
  2. Long lines of a message body (you can turn wordwrap on to fix this case)

Most clients are capable of handling QP encoding. If your client is not capable then you should upgrade your client or you must work within the above limitations to prevent the QP encoding from occuring.

Sometimes I get multiple (duplicate) messages sent when I call SendMail. Why?




AspMail has an internal routine to look for errors. If an error occurs at any point (even after the text of the message has been sent to the SMTP server) AspMail will go ahead and attempt to send it a second time in an attempt to complete the SMTP transaction without any errors if, and only if, you have multiple RemoteHosts set up in the RemoteHost property. The AspMail routine will only attempt 1 retry in this case.

If you only have one host name setup then the problem will be found in 

#1) you are calling SendMail more than once or 

#2) you are not handling cases where the user does a double/triple-click on his/her browser.

#3) Your smtp server is erroring out before the message transfer is complete in which case AspMail detects the error and resends till it gets a successful send (this can only occur with AspQMail) 

With AspMail can I have "required fields" on my HTML forms or other form processing functionality. Can AspMail change the order I get form field data back?



  1. Microsoft's ASP, not AspMail controls form data order. If you are looping through the form data in ASP order it probably won't be the original form order. You'll have to hard code field names to force the data back in a specific or original form order.
  2. Required fields are a function of HTML and client side scripting, not AspMail. If you used a CGI library in the past that handled those issues for you, it what only because they delved into areas outside of SMTP/Mail. AspMail's only function in life is to send mail. It does not handle client side HTML/scripting.
"Can someone use AspMail to send SPAM through our system?" AspMail does not accept SMTP connections and therefore cannot be used to "relay" SMTP traffic. AspMail can generate new SMTP message traffic but only if you provide a script page to generate it. Therefore it is under the Web operator's control.
I am getting blank emails


We are getting blank emails even when we aren't calling/using AspMail

One of your scripts which calls AspMail is getting called

  1. By someone using a Web browser or 
  2. By a stats package like WebTrends or 
  3. By a Web Robot which is parsing your site.

AspMail is a component and it doesn't have the "brains" to generate messages on it's own cognizance.

I'm mailing a large number of people. At some point in the mailing it fails. Can you give me any tips? The first tip is to use the SMTPLog property to write an SMTP log to disk and see what the point of failure is based on the log. The SMTP server is probably rejecting your message for some reason and the cause is typically determinable from the log.

Secondly, some SMTP servers only accept mail for locally known recipients. The SMTP server may be rejecting your message since it is being addressed to recipients outside the domain of that SMTP server. This is unusual but could possibly be the problem you are running into.
What are some of the issues that affect the speed of the mailing?

A small message should take 3-4 seconds to send if the server is local and all other conditions are normal. The following list are some items that can affect transfer speed.

  1. The slower the TCP connection the longer the mailing will take. Typically a local server is much faster than a remote server. If you are losing packets between your server and the SMTP server you can expect longer times due to this packet loss.
  2. The size of the mailing. The larger the message the longer the message transfer will take to send.
  3. CPU utilization: If your CPU is experiencing heavy usage you can expect it to take longer to transfer mail.
  4. SMTP server utilization: If your SMTP server is experiencing heavy traffic response times from the server are going to take longer thus increasing the time it takes to send a message.

One thing we've found is that by changing the RemoteHost name from a hostname to an IP address can cut 3 or 4 seconds off some messages. Even if the RemoteHost name is set to the local server's name, we've found some messages waiting for the hostname lookup. Just change the address to an IP and you may see an increase in the speed.

Is there a limit to the length of email addresses. AspMail support longer email addresses than anyone has so there are no practical limits. If your email is not being delivered see the previous questions for pointers on how to troubleshoot mail failures.

top of page

AspMail Component Properties


The message body text. To clear the text once you have set it use the ClearBodyText Method.
Mailer.BodyText = "Your order for 15 widgets has been processed"
CharSet The character set. By default the char set is US Ascii
Valid values:
* 1 = US ASCII
* 2 = ISO-8859-1
Mailer.CharSet = 2

The ContentType property allows you to set the ContentType header of the message's BodyText. If, for example, you wanted to send HTML as the messages's body, you could set ContentType = "text/html" and EMail programs that support HTML content could properly display the HTML text.
Note: The ContentType property is ignored if you have file attachments.
Mailer.ContentType = "text/html"

CustomCharSet If you wish to use a character set besides the included types you can set CustomCharSet to a character set string.
Mailer.CustomCharSet = "ISO-2022" or
Mailer.CustomCharSet = "big5"
DateTime AspMail will, by default, create a Date/Time header for your local system using GMT. If you would like to override the date/time calculation set the DateTime property to a valid date/time string in the format defined by RFC 822 & RFC 1123.
Mailer.DateTime = "Fri, 02 May 1997 10:53:49 -0500"




The encoding type for attachments. The default setting is MIME.
Valid values:
* 1 = UUEncoded
* 2 = MIME

Mailer.Encoding = 1

Expires If the component is an eval version the expires property will return the date that the component quits functioning.
Response.Write "Component Expires: " & Mailer.Expires


top of page

The message originator’s name.
Mailer.FromName = "Joe’s Widget Shop"
FromAddress The message originator’s email address.
Mailer.FromAddress = ""
Organization Sets the Organization header in the message.
Mailer.Organization = "Your Company Name"

Sets the message priority. Priorities are 1-5 and are reflected in the X-Priority
Valid values:
* 1 – High
* 3 – Normal
* 5 – Low

Mailer.Priority = 1


The remote SMTP host that the message will be sent through. This is typically an SMTP server located at your local ISP or it could be an internal SMTP server on your companies premises. Up to 3 server addresses can be specified, separated by a semicolon. If the primary server is down the component will attempt to send the mail using the secondary server and so on.

If your RemoteHost uses another port besides 25 then append a colon and port number to the RemoteHost value.

Mailer.RemoteHost = "" or
Mailer.RemoteHost = ";" or
Mailer.RemoteHost = ""


The ReplyTo property allows you to specify a different email address that replies should be sent to. By default mail programs should use the Reply-To: header for responses if this header is specified.


The Response property returns any error messages that may occur.




If you need to debug the session give a log file name here. Make sure the IUSR_XYZ IIS user has security that allows the component to write to this file. Warning: Do not use this setting in situations where multiple users can access this component at the same time. This is for single user debugging ONLY!

Mailer.SMTPLog = "c:\smtplog.txt"

Subject The message subject.
Mailer.Subject = "Stock split announced!"

The SuppressMsgBody property is true by default and is used in conjunction with the SMTPLog property. When SMTPLog is set to a file and SuppressMsgBody is true the log file receives a copy of the message text. If SuppressMsgBody is false the message text is not sent to the log.

WordWrap The WordWrap property is off by default. Setting WordWrap to true causes the message body to wordwrap at the position specified by the WordWrapLen property.

The WordWrapLen property is set to 70 by default. You can modify the position that wordwrap occurs by changing this value.

top of page

AspMail Component Methods
Parameters Return Value Description


None True or False
if Mailer.SendMail then
Response.Write "Mail sent..."
Response.Write "Mail failure. Check mail host server name and tcp/ip connection..."
end if
The SendMail method attempts to send the email.
AddRecipient Mailer.AddRecipient "Jay Jones", "" True/False based upon success or failure. Adds a new recipient, as shown in the message's To: list.


None Clears any recipients assigned to the To list.
AddCC Mailer.AddCC "Jay Jones", "" True/False based upon success or failure. Adds a new recipient, as shown in the message's CC list.
ClearCCs None None Clears any recipients assigned to the CC list.
AddBCC Mailer.AddBCC "Jay Jones", "" True/False based upon success or failure. Adds a new Blind Carbon Copy recipient. BCC recipients are not shown in any message recipient list.
ClearBCCs None None Clears any recipients assigned to the BCC list.
ClearAllRecipients None None Clears all recipients assigned to the To, CC and BCC lists.
ClearBodyText None None Clears any text assigned to the message’s body which may have been set previously by using the BodyText property.

top of page