ANSMTP Developers Center > Using ANSMTP in Visual C#.NET

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.

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 "ANSMTP SMTP COMPONENT BUILD".

Declare and define objects

using ANSMTPLib;
....
private OBJClass m_oSmtp = new OBJClass();

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.
  _IEventThreadObjEvents_OnClosedEventHandler OnClosedEventHandler = 
               new _IEventThreadObjEvents_OnClosedEventHandler( OnClosed );
  m_oSmtp.OnClosed += OnClosedEventHandler;
			
  _IEventThreadObjEvents_OnConnectedEventHandler OnConnectedEventHandler = 
               new _IEventThreadObjEvents_OnConnectedEventHandler( OnConnected );
  m_oSmtp.OnConnected += OnConnectedEventHandler;

  _IEventThreadObjEvents_OnErrorEventHandler OnErrorEventHandler =
               new _IEventThreadObjEvents_OnErrorEventHandler( OnError );
  m_oSmtp.OnError += OnErrorEventHandler;

  _IEventThreadObjEvents_OnSendingEventHandler OnSendingEventHandler = 
               new _IEventThreadObjEvents_OnSendingEventHandler( OnSending );
  m_oSmtp.OnSending += OnSendingEventHandler;

  _IEventThreadObjEvents_OnAuthenticatedEventHandler OnAuthenticatedEventHandler = 
          new _IEventThreadObjEvents_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 MassSender 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.



2001-2008 © Copyright AdminSystem Software Limited. All rights reserved.   About us  Site Map