Get extended error information when an error 11004 occurs using TIdSmtp.Connect() method?

delphi, email, indy, smtp

Solution

`EIdSMTPReplyError` is raised in response to an error message from the SMTP server at the SMTP protocol layer. `11004` is a socket/DNS error, not an SMTP error. There would be no additional error information available other than the basic OS error message (which you can get from `SysErrorMessage()`):

The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for.

Regarding `TIdSMTP.Send()`, you can look for `EIdSMTPReplyError` exceptions, eg:

try
  IdSMTP1.Connect;
  try
    IdSMTP1.Send(IdMessage1);
  finally
    IdSMTP1.Disconnect;
  end;
except
  on E: EIdSMTPReplyError do
    Application.MessageBox(
      PChar(
        'Error message: ' + E.Message + sLineBreak +
        'Error code: ' + IntToStr(E.ErrorCode) + sLineBreak +
        'Error reply: ' + E.EnhancedCode.ReplyAsStr
      ),
      'SMTP Error...', MB_OK + MB_ICONSTOP + MB_TOPMOST);
end;

That will help some, and if the server supports Extended Error Codes then you might get a little bit closer. But more times then not, you will not likely be able to distinquish EXACTLY what went wrong without looking at the actual SMTP command/response traffic over the wire.

Regarding SSL/TLS errors in particular, one of several different things may happen, depending on your `TIdSMTP` configuration:

1) you might get a general `EIdSocketError` exception raised, like with your `11004` error.

2) You might get an `EIdSMTPReplyError` exception with a malformed error code if you connect to a server port that requires Implicit SSL/TLS but you do not have `TIdSMTP.UseTLS` set to `utUseImplicitTLS`.

2) You might get an `EIdOSSLConnectError` or other OpenSSL-related exception if you have `UseTLS=utUseImplicitTLS` assigned but the server is not expecting Implicit SSL/TLS.

3) You have `UseTLS` set to either `utUseExplicitTLS` or `utRequiresTLS` and the server supports Explicit TLS, but the SSL/TLS handshake fails for whatever reason, you will get a `TIdSMTP.OnTLSHandShakeFailed` event triggered only for `utUseExplicitTLS`. If the event handler does not set `VContinue=True`, or you used `utRequiresTLS`, you will get an `EIdTLSClientTLSHandShakeFailed` exception raised.

4) You have `UseTLS` set to either `utUseExplicitTLS` or `utRequiresTLS` and the server does not support Explicit TLS, you will get a `TIdSMTP.OnTLSNotAvailable` event triggered for `utUseExplicitTLS` only. If the event handler does not set `VContinue=True`, or you used `utRequiresTLS`, you will then get an `EIdTLSClientTLSNotAvailable` exception raised.

Problem

I have a Delphi 6 application that sends E-mails with attachments. When I first tested it I got an 11004 error (policy violation) when I called TIdSmtp.Connect(). It turned out my E-mail server SMTP setting were wrong and now it works fine. But know my users are going to run into trouble and I'd like to know if there is a way to get more extensive error information back from the SMTP server via the Indy components to help with my debugging efforts. I am using Indy 9 with Delphi 6. Is there a way to get a much richer report and find out what the probable cause for the error is? (e.g. - SSL required, From field not filled in, rejected domain, etc). Also, same question but when an error occurs on the TIdSmtp.Send() method?

Original source