Creating an Instance of the AspEmail Object

ASP

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

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

** Note from Tech Support: You MUST set the Mail.Host and Mail.From to domains we host. Your mail host is the SMTP for your domain that is on our mail server. The from address must be from a VALID email account from your domain name, also.**

<%
Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = "mail.yourdomain.com"
Mail.Username = "sales@yourdomain.com"
Mail.Password = "He11o@World!"
Mail.From = "sales@yourdomain.com"
Mail.FromName = "Sales Department"

Mail.AddAddress "mrjones@company1.com", "John Smith"
Mail.AddAttachment "e:\html\domains\yourdomaincom\html\filename.htm"

Mail.Subject = "Sales Receipt"
Mail.Body = "Dear John:" & chr(13) & chr(10) & _
" Thank you for your business."

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

or

The first thing you need to do is create a formpage.asp page with the code below:

<form name="YourFormName" method="Post" action="confirmation.asp">
< table>
< tr><td>Email: </td>
< td><input type="text" name="Email" size="50"></td></tr>
< tr><td>First Name: </td>
< td><input type="text" name="FirstName" size="50"></td></tr>
< tr><td>Last Name: </td>
< td><input type="text" name="LastName" size="50"></td></tr>
< tr><td>Subject: </td>
< td><input type="text" name="Subject" size="50"></td></tr>
< tr><td>Comments: </td>
< td><textarea name="Comments"></textarea></td>
< /table>
< input type="submit" name="Submit" value="Submit Form">
< /form>

Next, we create a confirmation.asp page with our ASPEmail code as seen below:

<%
DIM strEmail, strFirstName, strLastName, strSubject, strComments, Mail
strEmail = request.form("Email")
strFirstName = request.form("FirstName")
strLastName = request.form("LastName")
strSubject = request.form("Subject")
strComments = request.form("Comments")

DIM Mail, strMsgHeader
Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = "mail.aspwebpro.com"
Mail.From = strEmail
Mail.AddAddress "general@aspwebpro.com"
Mail.AddCC "general@aspwebpro.com"
Mail.Subject = "ASP Web Pro: Online Form"
strMsgHeader = "This email was delivered from the ASP Web Pro website." & vbCrLf & vbCrLf
Mail.Body = strMsgHeader & "Email: " & strEmail & vbCrLf & _
" First Name: " & strFirstName & vbCrLf & _
" Last Name: " & strLastName & vbCrLf & _
" Subject: " & strSubject & vbCrLf & vbCrLf & _
" Comments: " & vbCrLf & strComments
On Error Resume Next
Mail.Send
Set Mail = Nothing
IF Err <> 0 THEN
Response.Write "There has been an error and your message could not be sent through email. Please contact us directly at 1-716-768-6221. " & Err.Description
END IF
%>

<P>
< %
Response.Write strFirstName & ",<br>"
Response.Write "Your message has been successfully sent."
%>,</P>

Now you have a complete form that sends data to an email address and displays a customized message for your user. The default format for ASPEmail is to send plain text messages. If you want to send HTML messages with ASPEmail, you can simply include this extra line of code before the Mail.Body line.

Mail.isHTML = True

 

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

ASP.NET

AspEmail comes with an ASP.NET wrapper assembly, ASPEMAILLib.dll, which has to be placed in the \Bin subdirectory of your ASP.NET application. Alternatively, you can place this file in the Global Assembly Cache.

In C#, create an instance of AspEmail as follows:

<%@ Import Namespace="ASPEMAILLib" %>
<script runat="server" LANGUAGE="C#">
void Page_Load(Object Source, EventArgs E)
{
...
ASPEMAILLib.IMailSender objMail;
objMail = ASPEMAILLib.MailSender();
...
}
</script>

Essential Properties and Methods
In order to send email, AspEmail "talks" to an SMTP server. The SMTP server does not have to be running on the same machine as AspEmail, in fact it can be located anywhere on the local network or the Internet.

You must specify the address of your SMTP server via the Host property. The default port number for SMTP services is 25, but if your SMTP server runs on a different port, you must also specify it via the Port property:

Mail.Host = "smtp.mycompany.com" ' Required
Mail.Port = 25
' Optional. Port is 25 by default

You may also specify a comma- or semicolon-separated list of SMTP hosts, as follows:

Mail.Host = "smtp.domain1.com;smtp2.domain1.com;host.domain2.com"

If the first host on the list is down, AspEmail will automatically attempt to connect to the second host, etc. If none of the specified hosts are working, an error exception will be thrown.

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: the email 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."

or

' 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 "c:\dir\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.

<%
' change to address of your own SMTP server
strHost = "mail.elinkisp.com"

If Request("Send") <> "" Then
   Set Mail = Server.CreateObject("Persits.MailSender")
   ' enter valid SMTP host
   Mail.Host = strHost

   Mail.From = Request("From") ' From address
   Mail.FromName = Request("FromName") ' optional
   Mail.AddAddress Request("To")

   ' message subject
   Mail.Subject = Request("Subject")
   ' message body
   Mail.Body = Request("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 %>
<% 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>

Click the links below to run this code sample (ASP and ASP.NET versions, respectively):

http://localhost/aspemail/Simple/Simple.asp
http://localhost/aspemail/Simple/Simple.aspx .NET Version

The following code sample sends email in the HTML format. The script is essentially the same except that the message body is set to an HTML string, and the property IsHTML is set to True:

strHTML = message body in HTML format
...
Mail.IsHTML = True
Mail.Body = "<HTML><BODY><CENTER>" & strHTML & "</CENTER></BODY></HTML>"
...

Click the links below to run this code sample.

http://localhost/aspemail/HtmlFormat/HtmlFormat.asp
http://localhost/aspemail/HtmlFormat/HtmlFormat.aspx .NET Version