2

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.

Community
  • 1
  • 1
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249

2 Answers2

8

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • +1 Thanks, UseTLS=utUseImplicittTLS helped and now connect is executed, even send, but I don't get the email... Do you suggest something to check for? – UnDiUdin Jul 06 '12 at 16:05
  • 1
    If the server accepts the email data without error (ie, `Send()` does not raise an exception), then the server takes responsibility for delivering the email. If the email is not arriving then chances are that delivery failed somewhere along the way, long after you disconnected from the server. The only way to diagnose that is to check your inbox to see if the server is sending you an error message. Delivery errors that happen after the server's acceptance are outside of your SMTP session, which is why `TIdSMTP` cannot report them to you. – Remy Lebeau Jul 07 '12 at 07:53
  • I was able to read the delivery failed mail the error was "ccn not allowed", since it is mandatory to have a real (non cc or ccn) recipient, so i am using Recipients eplicitly and now it works. THanks a lot! – UnDiUdin Jul 10 '12 at 09:40
0

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

Mike Taylor
  • 2,376
  • 2
  • 17
  • 33