Send Email over SSL/TLS in Delphi

In previous section, I introduced how to send email in a simple Delphi project. In this section, I will introduce how to send email over SSL/TLS connection in Delphi.

SSL and TLS Introduction

SSL connection encrypts data between the SMTP component and SMTP server to protects user, password and email content in TCP/IP level. Now this technology is commonly used and many SMTP servers are deployed with SSL such as Gmail, Yahoo and Hotmail. There are two ways to deploy SSL on SMTP server:

  • Explicit SSL (TLS)

    Using STARTTLS command to switch SSL channel on normal SMTP port (25 or 587);

  • Implicit SSL

    Deploying SSL on another port (465 or other port, you may query it from your server administrator

EASendMail SMTP component supports both ways. The connection can be specified by Mail.ConnectType property. Please see the following example code.

TLS 1.2

TLS is the successor of SSL, more and more SMTP servers require TLS 1.2 encryption now.

If your operating system is Windows XP/Vista/Windows 7/Windows 2003/2008/2008 R2/2012/2012 R2, and you got connection error with SSL/TLS connection, you need to enable TLS 1.2 protocol in your operating system like this:

Enable TLS 1.2 on Windows XP/Vista/7/10/Windows 2008/2008 R2/2012

[Delphi - Send Email over SSL/TLS Setting - Example]

The following example codes demonstrates how to set SSL/TLS connection.

// Send email by normal TCP/IP without SSL connection
oSmtp.ServerAddr := 'localhost';
oSmtp.ServerPort := 25;

// Send email by SSL connection with STARTTLS command switching
oSmtp.ServerAddr := 'localhost';
oSmtp.ServerPort := 25;
oSmtp.ConnectType := 1;

// Send email by SSL connection with direct SSL.
oSmtp.ServerAddr := 'localhost';
oSmtp.ServerPort := 465;
oSmtp.ConnectType := 1;

// Send email by SSL/TLS connection with auto-detect.
// If port is 25 or 587, STARTTLS TLS will be used; otherwise direct SSL will be used.
oSmtp.ServerAddr := 'localhost';
oSmtp.ServerPort := 465;
oSmtp.ConnectType := 1;

oSmtp.ServerAddr := 'localhost';
oSmtp.ServerPort := 25;
oSmtp.ConnectType := 1;

// 4 means if server supports SSL/TLS connection, SSL/TLS is used automatically
oSmtp.ServerAddr := 'localhost';
oSmtp.ServerPort := 25;
oSmtp.ConnectType := 4;

oSmtp.ServerAddr := 'localhost';
oSmtp.ServerPort := 587;
oSmtp.ConnectType := 4;

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 over implicit SSL on 465 port - Example]

The following example codes demonstrate how to send email over SSL connection on 465 port.

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;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

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

var
  Form1: TForm1;

implementation
{$R *.dfm}

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 email body
  oSmtp.BodyText := 'this is a test email sent from Delphi project, do not reply';

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

  // Set SSL 465 port
  oSmtp.ServerPort := 465;

  // Set direct SSL connection
  oSmtp.ConnectType := ConnectSSLAuto;

  // 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';

  ShowMessage('start to send email ...');
  if oSmtp.SendMail() = 0 then
    ShowMessage('email was sent successfully!')
  else
    ShowMessage('failed to send email with the following error: '
    + oSmtp.GetLastErrDescription());

end;

end.

[Delphi - Send email over TLS (Explicit SSL) on 25 or 587 port - Example]

The following example codes demonstrate how to send email over TLS (STARTTLS command, Explicit SSL) connection on 25 port.

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;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

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 email body
  oSmtp.BodyText := 'this is a test email sent from Delphi project, do not reply';

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

  // Set 25 port, if your server uses 587 port, please change 25 to 587
  oSmtp.ServerPort := 25;

  // Set TLS connection
  oSmtp.ConnectType := ConnectSSLAuto;

  // 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';

  ShowMessage('start to send email ...');
  if oSmtp.SendMail() = 0 then
    ShowMessage('email was sent successfully!')
  else
    ShowMessage('failed to send email with the following error: '
    + oSmtp.GetLastErrDescription());

end;

end.

SMTP Server SSL Certificate

To send email over SSL/TLS connection, you don’t need to install a certificate on your machine. The data is encrypted by server certificate public/private key pair.

SMTP Setting for Gmail, Yahoo, Hotmail and Office 365

Because most popluar email providers support or require SSL/TLS connection, so I will introduce specific setting for Gmail, Yahoo, Hotmail and Office 365 in the coming sections.

Next Section

At next section I will introduce how to send email using Gmail account in Delphi.

Appendix

Comments

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