ASP E-mail

From SuperbHosting.net Support Wiki

Jump to: navigation, search

Contents

What is AspEmail?

AspEmail 5.0 is an active server component for sending e-mail messages using an external SMTP server in an ASP or VB environment. AspEmail 5.0 supports multiple recipients, multiple file attachments, HTML format, embedded images and sounds, non-US ASCII character sets, secure mail, and high-performance message queuing.


Creating an Instance of the AspEmail Object

To use AspEmail in an ASP environment, you must create an instance of the AspEmail object in your ASP script as follows:

ASP
<%
...
Set Mail = Server.CreateObject("Persits.MailSender")
...
%>
VB


To use AspEmail in a VB environment, open your VB project, go to Project/References, and check the box next to Persits Software AspEmail 5.0. Declare an AspEmail object variable as follows:

Dim Mail As MailSender


Create an instance of the AspEmail object as follows:

Set Mail = New MailSender

Essential Properties and Methods

In order to send email, AspEmail "talks" to an SMTP server. You must specify the address of your SMTP server via the Host property:

Mail.Host = "localhostā€


You must also specify the sender's email address and, optionally, name as follows:

Mail.From = "sales@mycompany.com" Required
Mail.FromName = "Sales Department" Optional


To add message recipients, CCs, BCCs, and Reply-To's, use the AddAddress, AddCC, AddBcc and AddReplyTo methods, respectively. These methods accept two parameters: E-mail address and (optionally) name. Notice that you must not use an '=' sign to pass values to the methods. For example:

Mail.AddAddress "jsmith@company1.com", "John Smith"
Mail.AddCC "bjohnson@company2.com" (Name is optional)


Use the Subject and Body properties to specify the message subject and body text, respectively. A body can be in a text or HTML format. In the latter case, you must also set the IsHTML property to True. For example:

  • Text Format
Mail.Subject = "Sales Receipt"
Mail.Body = "Dear John:" & chr(13) & chr(10) & "Thank you for your business.
Here is your receipt."


  • HTML format
Mail.Subject = "Sales Receipt"
Mail.Body = "<HTML><BODY BGCOLOR=#0000FF>Dear John:....</BODY></HTML>"
Mail.IsHTML = True


To send a file attachment with a message, use the AddAttachment method. It accepts the full path to a file being attached. Call this method as many times as you have attachments. Notice that you must not use the '=' sign to pass a value to the method:

Mail.AddAttachment "d:\home\user_name\receipt.doc"


To send a message, call the Send method. The method throws exceptions in case of an error. You may choose to handle them by using the On Error Resume Next statement, as follows:

On Error Resume Next
Mail.Send
If Err <> 0 Then
Response.Write "An error occurred: " & Err.Description
End If

Code Samples

The following code sample demonstrates a simple email-sending form:

<%
strHost = "mail.elinkisp.com" (Change to address of your own SMTP server)
If Request("Send") <> "" Then
Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = strHost (Enter valid SMTP host)
Mail.From = Request("From") (From address)
Mail.FromName = Request("FromName") (Optional)
Mail.AddAddress Request("To")
Mail.Subject = Request("Subject") (Message subject)
Mail.Body = Request("Body") (Message body)
strErr = ""
bSuccess = False
On Error Resume Next (Catch errors)
Mail.Send Send message
If Err <> 0 Then (Error occurred)
strErr = Err.Description
else
bSuccess = True
End If
End If
%>


<HTML>
<BODY BGCOLOR="#FFFFFF">
<% If strErr <> "" Then %>
<h3>Error occurred: <% = strErr %></h3>
<% End If %>
<% If bSuccess Then %>
Success! Message sent to <% = Request("To") %>.
<% End If %>
<FORM METHOD="POST" ACTION="Simple.asp">
<TABLE CELLSPACING=0 CELLPADDING=2 BGCOLOR="#E0E0E0">
<TR>
<TD>Host (change as necessary in script):</TD>
<TD><B><% = strHost %></B></TD>
</TR>
<TR>
<TD>From (enter sender's address):</TD>
<TD><INPUT TYPE="TEXT" NAME="From"></TD>
</TR>
<TR>
<TD>FromName (optional, enter sender's name):</TD>
<TD><INPUT TYPE="TEXT" NAME="FromName"></TD>
</TR>
<TR>
<TD>To: (enter one recipient's address):</TD>
<TD><INPUT TYPE="TEXT" NAME="To"></TD>
</TR>
<TR>
<TD>Subject:</TD>
<TD><INPUT TYPE="TEXT" NAME="Subject"></TD>
</TR>
<TR>
<TD>Body:</TD>
<TD><TEXTAREA NAME="Body"></TEXTAREA></TD>
</TR>
<TR>
<TD COLSPAN=2><INPUT TYPE="SUBMIT" NAME="Send" VALUE="Send Message">
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
Personal tools