ANSMTP Developers Center > Using ANSMTP in ASP/ASP.NET
Note: this article is only suitable for ANSMTP 6.4 or later.
For ANSMTP 6.3 or earlier version, please click here.
Important notice:* For C#, Visual Basic.NET, Managed C++, J#, JScript.NET and ASP.NET developers, we strongly recommend that you use the pure .NET class EASendMail SMTP Component instead of ANSMTP SMTP Component.
Introduction
ANSMTP is a SMTP component which supports all operations of SMTP/ESMTP protocols (RFC 821, RFC 822, RFC 2554). This tutorial covers the basics of sending email with ANSMTP in ASP/ASP.Net.
How to send email with SMTP server;
How to send email
without SMTP server (via DNS lookup).
Installation and Deployment
You should download the ansmtp installer and install it on your machine at first. If you want to distribute or deploy ansmtp without ansmtp installer, please click here to learn more.
Set up your IIS for ASP/ASP.NET application
Firstly, you need to install ANSMTP on your server. Secondly, create a virtual directory for your asp/asp.net application on IIS. Please note that you should assign this virtual directory with permission to run script. For ASP.NET, you have to create a sub-directory named "bin" under this virtual directory, and copy "AOSMTPLib.dll" to "bin" directory. You can find "AOSMTPLib.dll" in "DOTNET Assembly" sub-directory of ANSMTP installation directory("C:\Program Files\AdminSystem.NET\ANSMTP\DOTNET Assembly"). It is a run-time library of ANSMTP for .NET. If the .aspx is under IIS root or sub-directory, so you should create "bin" under "C:\Inetpub\wwwroot", and copy AOSMTPLib.dll to "C:\Inetpub\wwwroot\bin"
Create object instance
Now, we can use ANSMTP in ASP/ASP.NET more easily. The following code demonstrates how to create object instances in ASP/ASP.NET.
[ASP]
<%
Dim oSmtp
Set oSmtp = Server.CreateObject("AOSMTP.Mail")
%>
[ASP.NET/VB] <%@ Page language="VBScript" AutoEventWireup="true" aspcompat=true%> <%@ Import Namespace="AOSMTPLib"%> <% Sub Page_Load( sender As Object , e As System.EventArgs ) Dim oSmtp As AOSMTPLib.MailClass oSmtp = New AOSMTPLib.MailClass() End Sub %>
[ASP.NET/C#]
<%@ Page language="C#" AutoEventWireup="true" aspcompat=true%>
<%@ Import Namespace="AOSMTPLib"%>
<%
private void Page_Load( object sender, System.EventArgs e )
{
AOSMTPLib.MailClass oSmtp = new AOSMTPLib.MailClass();
}
%>
How to send email?
Now, we send email in ASP/ASP.NET with the following code.
[ASP]
<%
Set oSmtp = Server.CreateObject("AOSMTP.Mail")
oSmtp.ServerAddr = "127.0.0.1"
'If you don't have a SMTP server, use the following code
'send email via DNS lookup, ANSMTP lookups SMTP server automatically.
'oSmtp.ServerAddr = ""
oSmtp.FromAddr = "test@adminsystem.net"
oSmtp.AddRecipient "Support Team", "support@adminsystem.net", 0
oSmtp.Subject = "Test"
oSmtp.BodyText = "Hello, this is a test...."
If oSmtp.SendMail() = 0 Then
Response.Write "Message delivered!"
Else
Response.Write oSmtp.GetLastErrDescription()
End If
%>
[ASP.NET/C#]
<%@ Page language="C#" AutoEventWireup="true" aspcompat=true%>
<%@ Import Namespace="AOSMTPLib"%>
<%
private void Page_Load( object sender, System.EventArgs e )
{
AOSMTPLib.MailClass oSmtp = new AOSMTPLib.MailClass();
oSmtp.ServerAddr = "127.0.0.1";
//If you don't have a SMTP server, use the following code
//send email via DNS lookup, ANSMTP would lookup SMTP server automatically.
//oSmtp.ServerAddr = "";
oSmtp.FromAddr = "test@adminsystem.net";
oSmtp.AddRecipient( "Support Team", "support@adminsystem.net", 0 );
oSmtp.Subject = "Test";
oSmtp.BodyText = "Hello, this is a test....";
if( oSmtp.SendMail() == 0 )
Response.Write( "Message delivered!" );
else
Response.Write( oSmtp.GetLastErrDescription());
}
%>
More summary
If you develop ASP.NET application with Visual Studio.NET, please refer to the corresponding tutorials. You may also refer to ASP/ASP.NET samples in ANSMTP installation package.
Related Links
Email Queuing with Database
Email Queueing with MSMQ
IIS SMTP Service
Free Email Support
Not enough? Please contact our technical support team.
Support@EmailArchitect.NET
VIP@EmailArchitect.NET(Registered
User)
Remarks
We usually reply emails in 24hours. The reason for getting no response is
likely that your smtp server bounced our reply. In this case, please try to use
another email address to contact us. Your Hotmail or Yahoo email account is
recommended.
|