Indy and smtps: cannot connect

2019-07-10 00:17发布

问题:

I am trying to send emails from smtps (secure smtp) using Indy and the technique exaplined in this Marco Cantù article.

This is what I am using:

object SMTP: TIdSMTP
  IOHandler = IdSSLIOHandlerSocketOpenSSL1
  SASLMechanisms = <>
  UseTLS = utUseExplicitTLS
  Left = 32
  Top = 196
end

and

SMTP.Host := 'smtps.pec.aruba.it';;
        SMTP.Port := 465;;
        SMTP.Username := 'myaddress@pec.it';
        SMTP.Password := 'myPassw0rd'; 
        MailMessage.Encoding := meDefault;
        MailMessage.From.Address := 'myaddress@pec.it';

        MailMessage.BccList.EMailAddresses := 'testaddress0@gmail.com';
        MailMessage.Subject := 'Test Mail';
        MailMessage.Body.Text := 'Please ignore this mail, This is a test';

        SMTP.Connect; //failure!!!
        SMTP.Send(MailMessage);

i program hangs on SMTP.Connect, but without any exception or useful error.

If instead than aboe i use gmail setings as explained in the article all works

Can you please give an advice?

i have Indy 10.5.8 and the ssl dlls in the same path as the exe.

回答1:

Connect() hangs because you are connecting to the wrong Port with the wrong UseTLS property assignment.

Port 465 is the implicit SSL port for SMTP, meaning the client must encrypt the connection immediately upon connecting to the server before any SMTP-related communication can occur. The server is expecting your client to send an SSL handshake, but your client is not doing that because you set UseTLS=utUseExplicitTLS. That tells TIdSMTP to expect the connection to be initially unencrypted upon connecting to the server and then TIdSMTP can send an explicit STARTTLS command to the server to activate TLS encryption dynamically when needed.

Thus, TIdSMTP is waiting for the server to send an SMTP greeting, which it never does, and the server is expecting your client to send an SSL handshake, which it never does. A deadlock occurs until one of the parties disconnects.

If you connect to port 465, you must set UseTLS=utUseImplicitTLS instead. To use UseTLS=utUseExplicitTLS, you need to connect to port 25 or 587 instead.



回答2:

Many mail servers delay the accepting of a connection in order to prevent mailer bots bombarding them with attempts to deliver SPAM mail.

It maybe the hang you are seeing is just a long delay in the connection, i have used these components before with many different servers and connect will always return an exception on an error. if it was me i would now be using a packet sniffer such as wireshark to check what is going on