Home > Mobile >  Problem sending an email via GMail with Indy SMTP client
Problem sending an email via GMail with Indy SMTP client

Time:02-11

Can someone show me what I am doing wrong (or not doing)?

Here is my procedure code:

procedure TForm20.SendImage(const Comment, AImage: String);
var
  SMTP: TIdSMTP;
  Msg: TIdMessage;
begin
  Msg := TIdMessage.Create(nil);
  try
    Msg.From.Address := '[email protected]';
    Msg.Recipients.EMailAddresses := '[email protected]';
    Msg.Body.Text := 'hello dennis';
    Msg.Subject := 'free space';

    SMTP := TIdSMTP.Create(nil);
    try
      SMTP.Host := 'smtp.gmail.com';
      SMTP.Port := 587;  
      //SMTP.Port := 465; //gives different error-connection closed gracefully but
      //no email is sent or received
         
      SMTP.AuthType := satDefault;
      SMTP.Username := '[email protected]';
      SMTP.Password := '$$$$$$$';

      SMTP.Connect;
      //fails with port 25- connection time out socket error
      //with port 465 it never gets past smtp connect.  shows 'closed graceflly'
      //with port 587 it gets past connect but then says
      //'must issue a STARTTLS command first' before smtp.send(msg)

      SMTP.Send(Msg);
    finally
      SMTP.Free;
    end;
  finally
    Msg.Free;
  end;
end;

CodePudding user response:

The problem is that you are not setting the TIdSMTP.UseTLS property.

On port 25 and 587, you must set UseTLS to utUseExplicitTLS, then TIdSMTP will issue a STARTTLS command to initiate an SSL/TLS handshake before sending emails.

On port 465, you must set UseTLS to utUseImplicitTLS, then TIdSMTP will initiate an SSL/TLS handshake immediately upon connecting, before reading the server's greeting.

Either way, using SSL/TLS requires assigning a TIdSSLIOHandlerSocketBase-derived component, such as TIdSSLIOHandlerSocketOpenSSL, to the TIdSMTP.IOHandler property before connecting to the server, eg:

var
  IO: TIdSSLIOHandlerSocketOpenSSL;
...
IO := TIdSSLIOHandlerSocketOpenSSL.Create(SMTP);
IO.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
// configure other IO properties as needed...

SMTP.IOHandler := IO;

Note that TIdSSLIOHandlerSocketOpenSSL does not support TLS 1.3 . At this time, if you need to use TLS 1.3 then try this work-in-progress SSLIOHandler for that purpose.

  • Related