Send HTML Email in Delphi

In previous section, I introduced how to send email without specified SMTP server. In this section, I will introduce how to compose and send HTML email in Delphi.


If you want to specify the font, color or insert pictures in your email, you should use Html email format instead of Plain text email format.


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 HTML email - Example]

The following example codes demonstrate how to send email in HTML body format in Delphi.


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

unit Unit1;
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, EASendMailObjLib_TLB; // add EASendMail unit
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    { Private declarations }
    { Public declarations }

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

  Form1: TForm1;


{$R *.dfm}

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

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

  // Set email subject
  oSmtp.Subject := 'test HTML email from Delphi project';

  // Set HTML body format
  oSmtp.BodyFormat := 1;
  // Set HTML body text
  oSmtp.BodyText := '<font size=5>This is</font> <font color=red><b>a test</b></font>';

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

  // User and password for ESMTP authentication, if your server doesn't require
  // user authentication, please remove the following codes
  oSmtp.UserName := '';
  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

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



After you received the email by your email client, the body text is like this:

send HTML email in delphi

Import Html to email directly

You don’t have to write the HTML source body text in your application manually. You can build a html file with HTML tools and use ImportMailEx method to import the html file directly.

You can also refer to the htmlmail.* samples in EASendMail Installer. Those samples demonstrate how to build a HTML email editor and send HTML email with attachment or embedded images/pictures.

Delphi html editor

Next Section

At next section I will introduce how to attach file attachment to email message.



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