Delphi to send email using Gmail SMTP server

April 9, 2010

You can send email by utilizing Gmail smtp server. In Delphi, you can use Indy Component. The instructions here are based on Delphi 6 and OpenSSL library with Indy 10.

Do these steps:

1. Download Indy 10 from http://www.indyproject.org/. Then extract the zip file.
2. Download OpenSSL (version 1 or later) from http://www.indyproject.org/. Then extract the zip file.
3. Open and compile .dpk files in the following order:
a) IndySystemX0.dpk (in Lib\System)
b) IndyCoreX0.dpk (in Lib\Core)
c) IndyProtocolsX0.dpk (in Lib\Protocols)
4. Now open these .dpk files and click install in the following order:
a) dclIndyCoreX0.dpk (in Lib\Core)
b) dclIndyProtocolsX0.dpk (in Lib\Protocols)
5. In your Indy directory you should now see some compiled .dcu files. Open your Delphi IDE and goto the menu Tools>Environment options> Select Library tab. Now add the path to your .dcu files into the filepath collection. Click Ok.
6. Copy the two DLL files extracted in step 2 to your application directory.
7. Finally, here is the code of a demo program, with the DFM file and the actual Delphi code (modify it to suit yours):

object IdSMTP1: TIdSMTP
OnStatus = IdSMTP1Status
IOHandler = IdSSLIOHandlerSocketOpenSSL1
Host = 'smtp.gmail.com'
Password = '***'
Port = 587
SASLMechanisms = <>
UseTLS = utUseExplicitTLS
Username = '****'
end
object IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL
Destination = 'smtp.gmail.com:587'
Host = 'smtp.gmail.com'
MaxLineAction = maException
Port = 587
SSLOptions.Method = sslvTLSv1
SSLOptions.Mode = sslmUnassigned
SSLOptions.VerifyMode = []
SSLOptions.VerifyDepth = 0
OnStatusInfo = IdSSLIOHandlerSocketOpenSSL1StatusInfo
end
IdSMTP1.Connect;
IdSMTP1.Send(IdMessage1);
IdSMTP1.Disconnect;

8. Done. It works great for me!