Samples
Shown below are sample use of the easy2email SMTP component in IIS ASP pages (sample 1 & 2) and IIS ASP.NET pages (sample 3). Feel free to adapt to your needs.

Sample 1
The example demonstrates the sending of a single plain text message after authenticating to the SMTP server. It also shows the use of the log reference property. Server-side VBScript is used. Note: In this example activities are logged to a text file. If you want to disable logging then call "SetLogging lgNone".

<HTML>
<HEAD>
<TITLE>easy2email Send Test Page</TITLE>

<STYLE>
.SuccessText
{
FONT-SIZE: 11px;
COLOR: #000000;
FONT-FAMILY: Verdana
}
.ErrorText
{
FONT-SIZE: 11px;
COLOR: #ff0000;
FONT-FAMILY: Verdana
}
</STYLE>
</HEAD>
<BODY>
<P class=SuccessText>Results of sending email with easy2email</P>
<UL>

<%

'---- Message Priority Enum Values (could also be in separate include file) ----
Const mpHighest = 0
Const mpHigh = 1
Const mpNormal = 2
Const mpLow = 3
Const mpLowest = 4
'---- Logging Enum Values (could also be in separate include file) ----
Const lgNone = 0
Const lgText = 1
Const lgADODB = 2

'---- Create the mail object and set up host and port number----
Set Mail = Server.CreateObject("easy2email.SMTP")
Mail.Host = "smtp.myserver.com"
Mail.Port = 25

'---- Set up logging of activities of component - optional ----
'---- Set up IgnoreLoggingExceptions and LoggingDestination before calling SetLogging ----
'---- IgnoreLoggingExceptions=False is useful for debugging ----
Mail.IgnoreLoggingExceptions = True
Mail.LoggingDestination = "c:\logdata\file1.txt"
Mail.SetLogging lgText

'---- Set up username and password to authenticate to mail server ----
'---- that is if mail server requires authentication. If not set Username='' ----
Mail.Username = "johns"
Mail.Password = "password"

'---- Catch errors
On Error Resume Next

'---- Connect to mail server (no authentication yet) ----
ConnectResult = Mail.Connect
If ConnectResult Then

'----- Authenticate to mail server using username and password ----
  AuthenticateResult = Mail.Authenticate
  If AuthenticateResult Then

'---- Set up MIME content type for body of message use text/plain for a plain text message ----
    Mail.ContentType = "text/plain"

'---- Set up from, recipient and other details ----
    Mail.Priority = mpNormal
    Mail.FromEmail = "johnsmith@mydomain.com"
    Mail.FromName = "John Smith"
    Mail.AddRecipient "joebloggs@somedomain.com", "Joe Bloggs"

'---- Set up Subject and body of message
    Mail.Subject = "A test message from easy2email"
    MailBody = "A simple plain text test message" & vbCrLf
    MailBody = MailBody & "You can add more text or whatever" & vbCrLf
    Mail.Body = MailBody

' ---- LogReferenceID is an optional field for your use. It could be an order ----
' ---- number, customer number etc. It is logged with the activity in log file or database. ----
    Mail.LogReferenceID = "JBL00157"
    Mail.Send

'---- Error check ----
    If Err <> 0 Then
      Response.Write "<li><span class=ErrorText>Send Error, Description: " & Err.Description & "</span></li>"
    Else
      Response.Write "<li><span class=SuccessText>Message sent to " + RecipientName + "</span></li>"
    End If

  Else
    Response.Write "<li><span class=ErrorText>Authentication Error, Description: " & Err.Description & "</span></li>"
  End If

  Mail.Disconnect
Else
  Response.Write "<li><span class=ErrorText>Connect Error, Description: " & Err.Description & "</span></li>"
End If
Set Mail = Nothing

%>
</UL>
</BODY>
</HTML>


Sample 2
The example demonstrates the sending of two html messages. Activities are logged with ADODB to a Microsoft Access Database with the database connection properties specified in a .udl file called logdata.udl. Please see our FAQ section for more on .udl files. Server-side VBScript is used.

<HTML>
<HEAD>
<TITLE>easy2email Send Test Page</TITLE>

<STYLE>
.SuccessText
{
FONT-SIZE: 11px;
COLOR: #000000;
FONT-FAMILY: Verdana
}
.ErrorText
{
FONT-SIZE: 11px;
COLOR: #ff0000;
FONT-FAMILY: Verdana
}
</STYLE>
</HEAD>
<BODY>
<P class=SuccessText>Results of sending email with easy2email</P>
<UL>

<%

'---- Message Priority Enum Values (could also be in separate include file) ----
Const mpHighest = 0
Const mpHigh = 1
Const mpNormal = 2
Const mpLow = 3
Const mpLowest = 4
'---- Logging Enum Values (could also be in separate include file) ----
Const lgNone = 0
Const lgText = 1
Const lgADODB = 2

'---- Create the mail object and set up host and port number----
Set Mail = Server.CreateObject("easy2email.SMTP")
Mail.Host = "smtp.myserver.com"
Mail.Port = 25

'---- Set up logging of activities of component - optional ----
'---- Set up IgnoreLoggingExceptions and LoggingDestination before calling SetLogging ----
'---- IgnoreLoggingExceptions=False is useful for debugging ----
Mail.IgnoreLoggingExceptions = True
Mail.LoggingDestination = "c:\logdata\logdata.udl"
Mail.SetLogging lgADODB

'---- Set up username and password to authenticate to mail server ----
'---- that is if mail server requires authentication. If not set Username='' ----
Mail.Username = "johns"
Mail.Password = "password"

'---- Catch errors
On Error Resume Next

'---- Connect to mail server (no authentication yet) ----
ConnectResult = Mail.Connect
If ConnectResult Then

'----- Authenticate to mail server using username and password ----
  AuthenticateResult = Mail.Authenticate
  If AuthenticateResult Then

'---- First Email message ----
'---- Set up MIME content type for body of message use text/plain for a plain text message ----
    Mail.ContentType = "text/html"

'---- Set up from, recipient and other details ----
    Mail.Priority = mpNormal
    Mail.FromEmail = "johnsmith@mydomain.com"
    Mail.FromName = "John Smith"
    Mail.AddRecipient "joebloggs@somedomain.com", "Joe Bloggs"

'---- Set up Subject and body of message
    Mail.Subject = "A test message from easy2email to Joe"
    MailBody = "<p>A simple html message<br>" & vbCrLf
    MailBody = MailBody & "This text is on the next line.</p>" & vbCrLf
    Mail.Body = MailBody

'---- LogReferenceID is an optional field for your use. It could be an order ----
'---- number, customer number etc. It is logged with the activity in log file or database. ----
    Mail.LogReferenceID = "JBL00157"
    Mail.Send

'---- Error check ----
    If Err <> 0 Then
      Response.Write "<li><span class=ErrorText>Send Error, Description: " & Err.Description & "</span></li>"
    Else
      Response.Write "<li><span class=SuccessText>Message sent to " + RecipientName + "</span></li>"
    End If


'---- Second Email message ----
'---- IMPORTANT - note that you have to set everything up again as
'---- as the Send command clears recipient lists, message body etc.
'
'---- Set up MIME content type for body of message use text/plain for a plain text message ----
    Mail.ContentType = "text/html"

'---- Set up from, recipient and other details ----
    Mail.Priority = mpNormal
    Mail.FromEmail = "johnsmith@mydomain.com"
    Mail.FromName = "John Smith"
    Mail.AddRecipient "benbloggs@somedomain.com", "Ben Bloggs"

'---- Set up Subject and body of message
    Mail.Subject = "A test message from easy2email to Ben"
    MailBody = "<p>A simple html message<br>" & vbCrLf
    MailBody = MailBody & "This text is on the next line.</p>" & vbCrLf
    Mail.Body = MailBody

' ---- LogReferenceID is an optional field for your use. It could be an order ----
' ---- number, customer number etc. It is logged with the activity in log file or database. ----
    Mail.LogReferenceID = "BBL00129"
    Mail.Send

'---- Error check ----
    If Err <> 0 Then
      Response.Write "<li><span class=ErrorText>Send Error, Description: " & Err.Description & "</span></li>"
    Else
      Response.Write "<li><span class=SuccessText>Message sent to " + RecipientName + "</span></li>"
    End If
  Else
    Response.Write "<li><span class=ErrorText>Authentication Error, Description: " & Err.Description & "</span></li>"
  End If

  Mail.Disconnect
Else
  Response.Write "<li><span class=ErrorText>Connect Error, Description: " & Err.Description & "</span></li>"
End If
Set Mail = Nothing

%>
</UL>
</BODY>
</HTML>


Sample 3
The example demonstrates the sending of a single plain text message after authenticating to the SMTP server. This is a sample ASP.NET page using VB.NET. V1.1 or later of the .NET Framework is required. You would also need V1.0.2 or later of the easy2email component.

In the 2nd line of code easy2emaildn.SMTPClass is imported. easy2emaildn is a COM wrapper for the easy2email.dll COM server. The easy2emaildn.dll file is copied to the easy2email installation folder when the easy2email component is installed. It needs to be copied to the Bin folder of your ASP.NET application folder or you can add it to the .NET Global Assembly Cache.

The btn_SendMail sub is executed when the submit button is clicked after the details is filled in on the form. This sub redirects to a page called testcomplete.aspx with a success or error message after the email is sent (or not sent).

ASP server controls is used to validate user input and generate the appropriate client-side script for the browser used.

Note that if you create a virtual directory in IIS (for your application) also create an application from Properties - Virtual Directory tab, otherwise it may not be able to find the easy2emaildn.SMTPClass class.

The two files in this sample (test.aspx and testcomplete.aspx) can be found in the Samples folder under the easy2email installation folder after the component has been installed.

*** Code for test.aspx: ***

<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" Strict="True" %>
<%@ Import Namespace="easy2emaildn.SMTPClass" %>
<html>
<head>
<title>easy2email ASP.NET Send Test Page</title>
<style>
.SuccessText { FONT-SIZE: 10px; COLOR: #000000; FONT-FAMILY: Verdana }
.ErrorText{FONT-SIZE: 10px; COLOR: #ff0000; FONT-FAMILY: Verdana }
.LeftCol{font-family: Arial, Verdana, Sans-serif;font-size: 11px; width:155; margin-left: 30px;margin-right: 0px;margin-top: 0px;margin-bottom: 0px;}
.RightCol{font-family: Arial, Verdana, Sans-serif;font-size: 11px;margin-left: 0px;margin-right: 0px;margin-top: 0px;margin-bottom: 0px;}
.NormalInput{border-left: 1px solid #C0C0C0;border-right: 1px solid #C0C0C0;border-top: 1px solid #C0C0C0;border-bottom: 1px solid #C0C0C0;font-family: Arial, Verdana, Sans-serif;font-size: 11px;margin-width: 100%; margin-width: 100%;height=20px}
.GenText{font-family: Arial, Verdana, Sans-serif;font-size: 11px;}
.ProblemField{font-family: Arial, Verdana, Sans-serif;font-size: 10px; COLOR: #960000}
.BodyText{font-family: Arial, Verdana, Sans-serif;font-size: 11px;
  margin-left: 0px;margin-right: 0px;margin-top: 0px;margin-bottom: 0px;
  border-left: 1px solid #C0C0C0;border-right: 1px solid #C0C0C0;border-top: 1px solid #C0C0C0;border-bottom: 1px solid #C0C0C0;
  scrollbar-face-color: #8B79A3;
  scrollbar-darkshadow-color: #443E48;
  scrollbar-3dlight-color: #8A7E94;
  scrollbar-arrow-color: #E5E3E8;
  scrollbar-highlight-color: #E5E3E8;
  scrollbar-shadow-color: #CDCAD1;
  scrollbar-track-color: #CDCAD1;
  overflow: auto;
}
.SubmitButton{font-family: Arial, Verdana, Sans-serif;font-size: 11px;background: #CDCAD1;margin-left: 3px;margin-right: 3px;margin-top: 3px;margin-bottom: 3px; padding-left: 3px;padding-right: 3px;padding-top: 3px;padding-bottom: 3px; width:105px;
  border-left: 1px solid #C0C0C0;border-right: 1px solid #C0C0C0;border-top: 1px solid #C0C0C0;border-bottom: 1px solid #C0C0C0;}
.NoteTextBlack{font-family: Arial, Verdana, Sans-serif;font-size: 10px;color: #969696;}
.NoteTextRed{font-family: Arial, Verdana, Sans-serif;font-size: 10px;color: #960000;}

</style>
</head>

<script runat=server>

Sub btn_SendMail(sender As Object, e as System.EventArgs)

'---- Create the mail object and set up host and port number----
  Dim Mail As New easy2emaildn.SMTPClass
  Dim ErrorText As String
  ErrorText = "Success"
  Mail.Host = Request.Form("txtMailServer")
  Mail.Port = CInt(Request.Form("txtPort"))
  Mail.IgnoreLoggingExceptions = True

'---- For now disable logging performed by the easy2email component ----
  Mail.SetLogging(easy2emaildn.LoggingType.lgNone)

'---- Set up username and password to authenticate to mail server ----
'---- that is if mail server requires authentication. If not set Username='' ----
  Mail.Username = Request.Form("txtUserName")
  Mail.Password = Request.Form("txtPassword")

  Try
    Mail.Connect
  Catch ex As Exception
'---- Replace Linefeeds with spaces otherwise compiler freaks out ----
    ErrorText = "Connect Error: " & Replace(ex.Message,Chr(10),Chr(32))
    Response.Redirect("testcomplete.aspx?ErrorText=" & ErrorText)
  End Try

  Try
    Mail.Authenticate
  Catch ex As Exception
    ErrorText = "Authentication Error: " & Replace(ex.Message, Chr(10),Chr(32))
    Response.Redirect("testcomplete.aspx?ErrorText=" & ErrorText)
  End Try


'---- Set up MIME content type for body of message use text/plain for a plain text message ----
  Mail.ContentType = "text/plain"

'---- Set up from, recipient and other details ----
  Mail.Priority = easy2emaildn.MessagePriority.mpNormal
  Mail.FromEmail = Request.Form("txtFromEmail")
  Mail.FromName = Request.Form("txtFromName")
  Mail.AddRecipient(Request.Form("txtRecipientEmail"), Request.Form("txtRecipientName"))


'---- Set up Subject and body of message
  Mail.Subject = Request.Form("txtSubject")
  Mail.Body = Request.Form("txtBody")

'---- Send Message ----
  Try
    Mail.Send
  Catch ex As Exception
    ErrorText = "Send Error: " & Replace(ex.Message, Chr(10),Chr(32))
    Response.Redirect("testcomplete.aspx?ErrorText=" & ErrorText)
  End Try

  Mail.Disconnect
  Response.Redirect("testcomplete.aspx?ErrorText=" & ErrorText)
End Sub
</script>


<body bgcolor="#ffffff" text="#000000" link="#000080" vlink="#800080" alink="#9f9fca" ">
<a href="http://www.easy2email.com" target="_blank"><img src="./easy2emaillogo.gif" border="0"></img></a>
<p class="GenText">This ASP.NET page is provided to do a quick test of the easy2email SMTP component.</p>
<form runat="server">
<table cellspacing="0" cellpadding="0" width="100%" bgcolor="#ffffff" border="0">
  <tr valign="top">
    <td class="LeftCol">Mail Server Hostname/IP</td>
    <td class="RightCol"><input id="txtMailServer" name="txtMailServer" class="NormalInput" style="WIDTH: 257px; HEIGHT: 16px" size="45" RunAt="server"></input>
    <ASP:RequiredFieldValidator ControlToValidate="txtMailServer" Display="Dynamic" RunAt="server" ErrorMessage="<br> *Mail Server is Required*<br>" CssClass="ProblemField" />
    </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">SMTP Port</td>
    <td class="RightCol"><input id="txtPort" class="NormalInput" style="WIDTH: 49px; HEIGHT: 16px" size="6" name="txtPort" value="25" runat="server"></input>
      <ASP:RequiredFieldValidator ControlToValidate="txtPort" Display="Dynamic" RunAt="server" ErrorMessage="<br> *Port is Required*<br>" CssClass="ProblemField" />
    </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol"> </td>
    <td class="RightCol"> </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">Username*</td>
    <td class="RightCol"><input class="NormalInput" style="HEIGHT: 16px" size="30" name="txtUserName"> </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">Password* </td>
    <td class="RightCol"><input type="password" class="NormalInput" style="HEIGHT: 16px" size="30" name="txtPassword"> </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">  </td>
    <td class="RightCol">  </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">From Email Address </td>
    <td class="RightCol"><input id="txtFromEmail" class="NormalInput" style="HEIGHT: 16px" size="45" name="txtFromEmail" runat="server">
      <ASP:RegularExpressionValidator ControlToValidate="txtFromEmail"
      Display="Dynamic" RunAt="server" ErrorMessage="<br> *Email address invalid*<br>"
      ValidationExpression="^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$"
      CssClass="ProblemField" />
    </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">From Name </td>
    <td class="RightCol"><input class="NormalInput" style="HEIGHT: 16px" size="45" name="txtFromName"> </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">  </td>
    <td class="RightCol">  </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">Recipient Email Address </td>
    <td class="RightCol"><input id=txtRecipientEmail class="NormalInput" style="HEIGHT: 16px" size="45" name="txtRecipientEmail" runat="server">
      <ASP:RegularExpressionValidator ControlToValidate="txtRecipientEmail"
      Display="Dynamic" RunAt="server" ErrorMessage="<br> *Email address invalid*<br>"
      ValidationExpression="^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$"
      CssClass="ProblemField" />

    </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">Recipient Name </td>
    <td class="RightCol"><input class="NormalInput" style="HEIGHT: 16px" size="45" name="txtRecipientName"> </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">  </td>
    <td class="RightCol">  </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">Subject  </td>
    <td class="RightCol"><input class="NormalInput" style="WIDTH: 314px; HEIGHT: 16px" size="55" name="txtSubject"> </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">  </td>
    <td class="RightCol">  </td>
  </tr>
  <tr valign="top">
    <td class="LeftCol">Body </td>
    <td class="RightCol"><textarea class="BodyText" name="txtBody" rows="4" cols="59"></textarea></td>
  </tr>
  <tr valign="top">
    <td class="LeftCol"> </td>
    <td class="RightCol"> <br><input type="Submit" id="btnSubmit" OnServerClick="btn_SendMail" value="Send Email" name="btnSubmit" runat="server" class="SubmitButton" /></td>
  </tr>
</table> 
<p><span class="NoteTextBlack">* - Leave blank if the mail server does not require authentication.</span></p>
</form>

</body>
</html>


*** Code for testcomplete.aspx: ***

<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" Strict="True" %>

<script runat=server>
Sub Page_Load(ByVal sender As System.Object, e as System.EventArgs)
  Dim ResErrorText As String
  ResErrorText = Request.QueryString("ErrorText")
  If ResErrorText = "Success" Then
    ErrorMsg.Text = "Message successfully sent."
    ErrorMsg.CssClass = "SuccessText"
  Else
    ErrorMsg.Text = ResErrorText
    ErrorMsg.CssClass = "ErrorText"
  End If
End Sub

</script>

<html>
<head>
<title>easy2email ASP.NET Send Result Page</title>
<style>
.SuccessText { FONT-SIZE: 10px; COLOR: #000000; FONT-FAMILY: Verdana }
.ErrorText{FONT-SIZE: 10px; COLOR: #ff0000; FONT-FAMILY: Verdana }
</style>
</head>
<body>
<asp:Label id="ErrorMsg" RunAt="server" />
</body>
</html>
 
   
Home|Contact Us|Useful Links||Disclaimer  
 ©2007 easy2email. All rights reserved.