How To…Customize Indy Email Settings

Question

“How do I customize the TIdIndy settings to connect to a secure email server such as Gmail?”

 

Solution

Note: Starting with ReportBuilder 19, it is possible to skip the steps below and simply set the TppSMTPIndy.UseTLS property to True. It is still necessary to download and copy the proper SSL .dll files for this feature to function (link below). See the help topic for TppSMTPIndy for more information.

ppReport1.EmailSettings.Port := 587;
lEmail := TppEmail(ppReport1.Email);
TppSMTPIndy(lEmail.SMTP).UseTLS := True;

Note: Some email services may require a separate setting or password to allow emailing from an outside application.

The TppSMTPIndy plugin gives access to the TIdSMTP object with the IndySMTP property. From there you can customize the TIdSMTP settings to connect to virtually any email server available.

To gain access to the IndySMTP property simply typecast the TppEmail.SMTP property as a TppSMTPIndy object…

lEmail := TppEmail(ppReport1.Email);
TppSMTPIndy(lEmail.SMTP).IndySMTP.Port := 587;

Connecting to Gmail

Gmail requires TLS encryption from port 587 to send email from its server. To connect to the Gmail server with Indy components, you need to create a SSL IOHandler (TIdSSLIOHandlerSocketOpenSSL) to manage the encrypted connection.

Indy makes use of the OpenSSL libraries to connect to a SSL server. These libraries need to be installed on your machine before using the IOHandler. The latest OpenSSL libraries can be downloaded from the following web site…

http://indy.fulgan.com/SSL/

Once you download, simply copy the .dll files into your WINSYS\ directory or in your application path. As a rule of thumb, it is always a good idea to have the latest version of Indy and the OpenSSL libraries on your machine.

Below is an example of how you might send a report using Indy and Gmail.

Download: IndyGmail.zip

Sample Delphi code:

uses
  ppSMTPIndy10,
  ppEmail,
  IdSSLOpenSSL,
  IdExplicitTLSClientServerBase;

procedure TForm1.Button2Click(Sender: TObject);
var
  lEmail: TppEmail;
  lIOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin

  ppReport1.EmailSettings.UserName := 'username';
  ppReport1.EmailSettings.Password := 'password';
  ppReport1.EmailSettings.ShowEmailDialog := True;
  ppReport1.EmailSettings.HostAddress := 'smtp.gmail.com';

  lEmail := TppEmail(ppReport1.Email);

  lIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(Self);

  try
    lIOHandler.Destination := 'smtp.gmail.com:587';
    lIOHandler.Host := 'smtp.gmail.com';
    lIOHandler.Port := 587;

    TppSMTPIndy(lEmail.SMTP).IndySMTP.Port := 587;
    TppSMTPIndy(lEmail.SMTP).IndySMTP.IOHandler := lIOHandler;
    TppSMTPIndy(lEmail.SMTP).IndySMTP.UseTLS := utUseExplicitTLS;

    ppReport1.SendMail;

  finally
    lIOHandler.Free;
  end;

end;