Send Email with Embedded Images in Delphi

In previous section, I introduced how to send email with file attachment. In this section, I will introduce how to send email with embedded images in Delphi.

Introduction

To attach an embedded images to email, you should add an attachment to email at first. Then you should assign an unique identifier(contentid) to this attachment. Finally, you need to replace the <img src="your file name" /> to <img src="cid:yourcontentid" />.

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 embedded images - Example]

The following example codes demonstrate how to send email with embedded images 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;
  cid : WideString;
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 with embedded images';

  // Add embedded image and return the unique identifier of the attachment
  cid := oSmtp.AddInline('c:\test.jpg' );
  if cid = '' then
  begin
  ShowMessage( 'Failed to add embedded image with error: ' +
      oSmtp.GetLastErrDescription());
      exit;
  end;

  // Set HTML body format
  oSmtp.BodyFormat := 1;
  // Set HTML body
  oSmtp.BodyText := '<html><body>Hello, this is an embedded <img src="cid:' +
  cid + '"> picture.</body></html>';

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

To attach embedded images/pictures, ImportMailEx and ImportHtml methods are strongly recommended. With these methods, you don’t have to specify the ContentID manually. The html source/file html body can be imported to email with embedded pictures automatically.

[Delphi - Send email with embedded images - ImportHtml - Example]

The following example codes demonstrate how to send email using ImportHtml method with embedded images 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;
  cid : WideString;
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 with embedded images';

  // Set HTML body format
  oSmtp.BodyFormat := 1;

  // test.gif is in c:\my picture
  // test.gif will be imported as embedded image in the email
  oSmtp.ImportHtml( '<html><body>test <img src="test.gif"> importhtml</body></html>',
    'c:\my picture' );

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

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 sign email with digital certificate in Delphi.

Appendix

Comments

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