ANSMTP Developers Center > Using ANSMTP in Delphi

Note: this article is only suitable for ANSMTP 6.4 or later.
For ANSMTP 6.3 or earlier version, please click here.

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 Delphi.

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. Now start to create a standard delphi project: select menu "Project" -> "Import Type Libra"ry", checked "AOSMTP COMPONENT BUILD ..." and click "Create Unit".

Using ANSMTP in your project

The following code demonstrates how to declare and define objects in your project.

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, AOSMTPLib_TLB, StdCtrls, StrUtils;

....
var 
  m_oSmtp : TMail;

procedure TForm1.FormCreate(Sender: TObject);
begin
   m_oSmtp := TMail.Create(Application);
end;
					

Although you can create new object instance each time, we recommend declare object as member variable and create it in FormCreate subroutine so that you can reuse those member variables again without re-create it.

Simple Sample

The following code demonstrates how to send email delphi.

procedure TForm1.SendEmail();
var
  vServerAddr: OleVariant;
  vSubject: OleVariant;
  vBody: OleVariant;
  vSenderAddr: OleVariant;
  vRecipient: OleVariant;
  vError: OleVariant;  
begin
  if m_oSmtp = nil then
    m_oSmtp := TMail.Create(Application);
   
  vServerAddr := 'mail.adminsystem.net';
  //if you don't have a SMTP server, assign '' to ServerAddr,
  //ANSMTP will send email via DNS lookup.
  //vServerAddr := '';
  
  vSubject := 'test subject';
  vBody := 'test body';
  vSenderAddr := 'test@adminsystem.net';
  vRecipient := 'support@adminsystem.net';
  
  m_oSmtp.Reset();
  m_oSmtp.ServerAddr := vServerAddr;
  m_oSmtp.FromAddr := vSenderAddr;
  m_oSmtp.Subject := vSubject;
  m_oSmtp.BodyText := vBody;
  m_oSmtp.AddRecipient( vRecipient, vRecipient, 0 );  
  if m_oSmtp.SendMail() = 0 Then
    Application.MessageBox( 'Message delivered', '' );
  else
  begin
    vError := m_oSmtp.GetLastErrDescription();
    Application.MessageBox( vError, '', );
  end;
end;

Asynchronous mode and event driving

If you want to use IMail of ANSMTP in asynchronous mode, you MUST bind events like this:

procedure TForm1.BindEvents();
begin
  if m_oSmtp = nil then
  begin
    m_oSmtp := TMail.Create(Application);
    m_oSmtp.OnConnected := OnConnected;
    m_oSmtp.OnClosed := OnClosed;
    m_oSmtp.OnError := OnError;
    m_oSmtp.OnSending := OnSending;
    m_oSmtp.OnAuthenticated := OnAuthenticated;
    m_oSmtp.Asynchronous := 1; //asynchronous mode
  end;
end;

And you also need to define the following procedures to handle those events

procedure TForm1.OnConnected(Sender: TObject);
begin
  //add your implementation code
end;

procedure TForm1.OnClosed(Sender: TObject);
begin
  //add your implementation code
end;

procedure TForm1.OnError(Sender: TObject;
                          lError: Integer; const ErrDescription: WideString );
begin
  //add your implementation code
end;

procedure TForm1.OnSending(Sender: TObject; lSent: Integer; lTotal: Integer );
begin
  //add your implementation code
end;

procedure TForm1.OnAuthenticated(Sender: TObject);
begin
  //add your implementation code
end;

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

This sample demonstrates how to send email with asynchronous mode, click here to download.
Note: this sample is developed by Delphi 7.0.

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-2010 © Copyright AdminSystem Software Limited. All rights reserved.   About us  Site Map