Send Email with Attachment in Delphi

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

Introduction

To send an email with file attachment, we need to use AddAttachment method. This method can attach a file to the email message from local disk or a remote URL.

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 - Add attachment from local disk or remote URL - Example]

The following example codes demonstrate how to send email with attachment 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;

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

  // Add attachment from local disk
  if oSmtp.AddAttachment('c:\test.doc') <> 0 then
    ShowMessage('Failed to add attachment with error: ' +
    oSmtp.GetLastErrDescription());

  // Add attachment from remote website
  if oSmtp.AddAttachment('http://www.emailarchitect.net/webapp/img/logo.jpg') <> 0 then
    ShowMessage('Failed to add attachment with error: ' +
    oSmtp.GetLastErrDescription());

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

  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.

Next Section

At next section I will introduce how to add embedded images/pictures to email message in Delphi.

Appendix

Comments

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