tags:

views:

2054

answers:

1

Using D7 + Indy 10 latest build.

My code is using TIdSMTP to send email. I keep getting "Connection closed gracefully" at some end-users, and the email is never sent.

The code is like:

try
~~~~
~~~~
_idSMTP := TIdSmtp.Create;
with _idSMTP do
begin
  Host := 'myhost';
  Connect;
  try
    Send(_EmailMsg);
    Result := True;
  except
    on E: Exception do
    begin
      MsgDlgErr(Self.Handle, E.Message)
    end
  end;
end;
finally
 _idSMTP.Disconnect;
 _idSMTP.Free;
end;

Any advice?

+9  A: 

Read all about it on http://www.swissdelphicenter.ch/en/showarticle.php?id=1

EIdConnClosedGracefully is an exception signaling that the connection has been closed by the other side intentionally. This is not the same as a broken connection which would cause a connection reset error. If the other side has closed the connection and the socket is read or written to, EIdConnClosedGracefully will be raised by Indy. This is similar to attempting to read or write to a file that has been closed without your knowledge.

In some cases this is a true exception and your code needs to handle it. In other cases (typically servers) this is a normal part of the functioning of the protocol and Indy handles this exception for you. Even though Indy catches it, when running in the IDE the debugger will be triggered first. You can simply press F9 to continue and Indy will handle the exception, but the constant stopping during debugging can be quite annoying. In the cases where Indy catches the exception, your users will never see an exception in your program unless it is run from the IDE.

Lars Truijens
The exception doesn't occur in the IDE, but on end-user computers.
Atlas
Some virusscanners block outgoing connections on the SMTP port nowadays. This may cause the connection to be unresponsive and throw this error.
Stijn Sanders
@Atlas: That is why it says "In some cases..." :) The page also talks about the other cases.
Lars Truijens
The server could also be disconnecting the connection on its end if any error is detected in the message data or sender/recipient addresses as well.
Remy Lebeau - TeamB