ANSMTP Developers Center > Using ANSMTP in Visual C#.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 Visual C#.NET.
How to declare and define objects;
How to send email in synchronous mode;
How to send email in asynchronous mode;
How to send email with SMTP server;
How to send email
without specified 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.
Add reference of ANSMTP to your project
First of all, make sure ANSMTP has been installed on your machine. Then create a Windows Form project in Visual Studio.NET: select menu "Project" -> "Add Reference" -> COM," and select "AOSMTP COMPONENT BUILD ...".
Declare and define objects
using AOSMTPLib; .... private MailClass m_oSmtp = new MailClass();
Although you can create new object instance each time, we recommend declare object as member variable so that you just need to create it once and then enjoy reuse those member variables without re-create it again.
Simple Sample
The following code demonstrates how to send email in synchronous mode
private void SendEmail()
{
m_oSmtp.Reset();
m_oSmtp.FromAddr = "test@adminsystem.net";
m_oSmtp.ServerAddr = "mail.adminsystem.net";
//If you don't have a smtp server, assign "" to ServerAddr,
//ANSMTP will send email via DNS lookup
//m_oSmtp.ServerAddr = "";
m_oSmtp.Subject = "test subject";
m_oSmtp.BodyText = "test body";
m_oSmtp.AddRecipient("Support Team", "support@adminsystem.net", 0);
if( m_oSmtp.SendMail() == 0 )
MessageBox.Show( "Message delivered" );
else
MessageBox.Show( m_oSmtp.GetLastErrDescription());
}
Asynchronous mode and event driving
The following code demonstrates how to bind events of ANSMTP.
private void BindEvents()
{
m_oSmtp.Asynchronous = 1; // assign asynchronous mode
// Bind event to event handlers.
_IMailEvents_OnClosedEventHandler OnClosedEventHandler =
new _IMailEvents_OnClosedEventHandler( OnClosed );
m_oSmtp.OnClosed += OnClosedEventHandler;
_IMailEvents_OnConnectedEventHandler OnConnectedEventHandler =
new _IMailEvents_OnConnectedEventHandler( OnConnected );
m_oSmtp.OnConnected += OnConnectedEventHandler;
_IMailEvents_OnErrorEventHandler OnErrorEventHandler =
new _IMailEvents_OnErrorEventHandler( OnError );
m_oSmtp.OnError += OnErrorEventHandler;
_IMailEvents_OnSendingEventHandler OnSendingEventHandler =
new _IMailEvents_OnSendingEventHandler( OnSending );
m_oSmtp.OnSending += OnSendingEventHandler;
_IMailEvents_OnAuthenticatedEventHandler OnAuthenticatedEventHandler =
new _IMailEvents_OnAuthenticatedEventHandler( OnAuthenticated );
m_oSmtp.OnAuthenticated += OnAuthenticatedEventHandler;
}
You also need to define the corresponding methods to handle those events.
private void OnClosed()
{
//Add your implementation code
}
private void OnConnected()
{
//Add your implementation code
}
private void OnError( int lError, string ErrDescription)
{
//Add your implementation code
}
private void OnSending( int lSent, int lTotal )
{
//Add your implementation code
}
private void OnAuthenticated()
{
//Add your implementation code
}
Note*: You SHOULD NOT invoke SendMail method any more while ansmtp is sending an email, otherwise the email of sending would be terminated automatically.
To send multiple emails in concurrently, you should use FastSender object which enables an inner threading pool to send mass emails with high performance.
A Total Sample
Please refer to CSHARP sample in ANSMTP installation package.
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.
|