Home > Mobile >  Delphi XE Indy 10.6.2.0: TLS v1.2 hangs on IdSMTP.Connect;
Delphi XE Indy 10.6.2.0: TLS v1.2 hangs on IdSMTP.Connect;

Time:12-20

When trying to connect to an SMTP server with TLS v1.2 method and port 465 used, my program hangs infinitely. I created a test mail account on mailbox.org (TLS 1.2 obligatory) and tried with the provided data.

Environment: Delphi XE, Indy 10.6.2, OpenSSL 1.0.2u DLL files in program folder.

Source code:

try
   IdSMTPReport.IOHandler := FormMain.IdSSLIOHandlerSocketMail;
   IdSMTPReport.UseTLS := utUseExplicitTLS;
   IdSSLIOHandlerSocketMail.SSLOptions.Method := sslvTLSv1_2;

   IdSMTPReport.Host := ###;
   IdSMTPReport.Username := ###;
   IdSMTPReport.Password := ###;
   IdSMTPReport.Port := 465;

   IdMessageReport.ContentType := 'text/plain; charset=UTF-8';
   IdMessageReport.Sender.Address := IdMessageReport.From.Address;
   IdMessageReport.Sender.Name := IdMessageReport.From.Name;
   IdMessageReport.Recipients.Clear;
   IdMessageReport.Recipients.EMailAddresses := ###;
   IdMessageReport.Subject := 'Test';
   IdMessageReport.Body.Clear;
   IdMessageReport.Body := MailReport;
   IdSMTPReport.Connect;
   IdSMTPReport.Send(IdMessageReport);
   IdSMTPReport.Disconnect;
except
   try
      IdSMTPReport.Disconnect;
   except
   end;
end;

IdSSLIOHandlerSocketMail settings in Object Inspector

image

It hangs before sending any status text/info messages to the OnStatus/OnStatusInfo events.

The SMTP server works fine with the given credentials. If I try another server (1und1, German ISP) with TLS 1.2 and port 587, it is working fine. If I change to port 465, the timeout of 10000 ms (even with a higher timeout) takes effect without the mail being sent:

Status information:

Resolving hostname ###
Connecting to ###
Connected.
-> Timeout
Disconnected.

Any ideas?

CodePudding user response:

You are setting the TIdSMTP.UseTLS property to utUseExplicitTLS. That means TIdSMTP will connect to the server in an initially unencrypted state, read the server's greeting and capabilities, and then send a STARTTLS command to ask permission to send a TLS handshake to start a new encrypted session.

However, port 465 is SMTP'S implicit TLS port. That means the server will expect the client to perform a TLS handshake immediately upon connecting, before then exchanging any SMTP data, including the server's greeting.

So, you are in a catch-22 situation due to using a wrong configuration. By using utUseExplicitTLS, TIdSMTP is waiting for the server's unencrypted greeting. But, by using port 465, the server is waiting for the client's TLS handshake. So neither party is satisfying the other party's wait condition, hence the timeout.

SMTP's explicit TLS port is 587 instead. So, you need to:

  • use utUseImplicitTLS on port 465.

  • use utUseExplicitTLS on port 587.

Unless the server is configured differently.

  • Related