Send Email Asynchronously in Delphi

In previous section, I introduced how to encrypt email with digital certificate. In this section, I will introduce how to use event handler and send email asynchronously in Delphi.

Introduction

Asynchronous mode

In synchronous mode, once SendMail method is called, it returns to application after the method is complete. Therefore, if the runtime (it depends on the networking connection and the email size) is long, your application cannot do anything before this method ends, which results “my application is blocked or halted”. In contrast, in asynchronous mode, as SendMail method works in background, this methods return to application immediately no matter the running method is complete or not.

Event handler

In previous examples, after SendMail method is invoked, if you want to know the progress of the email sending, you should use Event Handler to monitor the progress of email sending.

Note

Remarks: All of samples in this section are based on first section: Send email in a simple Delphi project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[Delphi - Send email with event handler in asynchronous mode - Example]

To demonstrate how to use asynchronous mode and event handler, let’s add a TLabel control in the form at first, the name of the label is “Label1”.

The following example codes demonstrate how to send email with event handler in asynchronous mode.

Note

To get the full sample projects, please refer to Samples section.

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, EASendMailObjLib_TLB; // add EASendMail unit
type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);

    // Event handler procedure
    procedure OnAuthenticated(ASender: TObject);
    procedure OnConnected(ASender: TObject);
    procedure OnClosed(ASender: TObject);
    procedure OnError(ASender: TObject;
  lError: Integer; const ErrDescription: WideString);
    procedure OnSending(ASender: TObject; lSent: Integer; lTotal: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  ConnectNormal = 0;
  ConnectSSLAuto = 1;
  ConnectSTARTTLS = 2;
  ConnectDirectSSL = 3;
  ConnectTryTLS = 4;

var
  Form1: TForm1;
  m_bFinished : Boolean;
  m_bError : Boolean;

implementation

{$R *.dfm}

procedure TForm1.OnAuthenticated(ASender: TObject);
begin
  Label1.Caption := 'Authenticated';
end;

procedure TForm1.OnConnected(ASender: TObject);
begin
  Label1.Caption := 'Connected';
end;

procedure TForm1.OnClosed(ASender: TObject);
begin
  if not m_bError then
    Label1.Caption := 'email was sent successfully';
  m_bFinished := true;
end;

procedure TForm1.OnError(ASender: TObject;
  lError: Integer; const ErrDescription: WideString);
begin
  Label1.Caption := 'Failed to send email with error: ' + ErrDescription;
  m_bError := true;
  m_bFinished := true;
end;

procedure TForm1.OnSending(ASender: TObject; lSent: Integer; lTotal: Integer);
begin
  Label1.Caption := Format('Sending %d/%d ...', [lSent, lTotal]);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  oSmtp : TMail;
begin
  oSmtp := TMail.Create(Application);
  oSmtp.LicenseCode := 'TryIt';

  // Set your sender email address
  oSmtp.FromAddr := 'test@emailarchitect.net';
  // Add recipient email address
  oSmtp.AddRecipientEx('support@emailarchitect.net', 0);

  // Set email subject
  oSmtp.Subject := 'simple email from Delphi project';
  // Set body text
  oSmtp.BodyText := 'this is a test email sent from delphi';

  // Your SMTP server address
  oSmtp.ServerAddr := 'smtp.emailarchitect.net';

  // User and password for ESMTP authentication, if your server doesn't require
  // user authentication, please remove the following codes
  oSmtp.UserName := 'test@emailarchitect.net';
  oSmtp.Password := 'testpassword';

  // ConnectTryTLS means if server supports SSL/TLS connection, SSL/TLS is used automatically
  oSmtp.ConnectType := ConnectTryTLS;

  // If your server uses 587 port
  // oSmtp.ServerPort := 587;

  // If your server uses 25/587/465 port with SSL/TLS
  // oSmtp.ConnectType := ConnectSSLAuto;
  // oSmtp.ServerPort := 587; // 25 or 587 or 465

  m_bError := false;
  m_bFinished := false;

  // Set Asynchronous mode
  oSmtp.Asynchronous := 1;

  // Add event handler
  oSmtp.OnConnected := OnConnected;
  oSmtp.OnClosed := OnClosed;
  oSmtp.OnError := OnError;
  oSmtp.OnSending := OnSending;
  oSmtp.OnAuthenticated := OnAuthenticated;

  Button1.Enabled := false;
  Label1.Caption := 'start to send email ...';

  oSmtp.SendMail();
  // Wait for the result
  while not m_bFinished do
      Application.ProcessMessages();
  ShowMessage(Label1.Caption);
  Button1.Enabled := true;

end;

end.

Next Section

At next section I will introduce how to send mass emails with multiple threads in Delphi.

Appendix

Comments

If you have any comments or questions about above example codes, please click here to add your comments.