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.

Introduction

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.

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

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

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;
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 := '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 := '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';

  // If your SMTP server requires SSL connection, please add this line
  // oSmtp.SSL_init();

  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.

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.

Appendix

Comments

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