0

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 := 'frostydennis7@gmail.com';
    Msg.Recipients.EMailAddresses := 'trevosedennis@gmail.com';
    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 := 'frostydennis7@gmail.com';
      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;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    1) See if any of the responses in this thread help: https://stackoverflow.com/a/7717862/421195 2) Check your Delphi code against the Java/Python examples here: https://developers.google.com/gmail/api/guides/sending – paulsm4 Jan 26 '22 at 17:47

1 Answers1

2

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770