JBnew
JBnew

Reputation: 1

Problem reconnecting SMTP on one TIdSMTP instance

I use TIdSMTP to send emails. There have been no problems with any server up until now, however, we recently encountered a server where on the second iteration of establishing a connection I get an EIdConnClosedGracefully exception.

Of course, we can also send many messages on one connection and it works correctly, but unfortunately we also have to maintain a mechanism in this system where a new connection is established for each message, so we send the message and close the connection.

This all takes place on one TIdSMTP instance, and that's where we get an exception.

I have already checked and if I create a new TIdSMTP instance for each message, everything works fine.

Has anyone encountered such a problem before? Is a code necessary?

There is my code:

fIdSMTP:=TIdSMTP.Create;
fIdSSLIOHandlerSocketOpenSSL:=TIdSSLIOHandlerSocketOpenSSL.Create;
fIdSMTP.IOHandler:=fIdSSLIOHandlerSocketOpenSSL;

...
{ configuration }
...

procedure TMainMailSender.SendMessage(var 
  pSendingResult:Boolean);
var
  Sent: Boolean;
begin
  pSendingResult:=False;
  Sent:=False;
  try
    try
      if IsConnectionForEachMessage then
        begin
          if (not fIdSMTP.Connected) then
            fIdSMTP.Connect;
        end;
      try
        if fIdSMTP.Connected then
          begin
            fIdSMTP.Send(fIdMessage);
            Sent:=True;
          end;
      finally
        if IsConnectionForEachMessage then
          begin
            if fIdSMTP.Connected then
              fIdSMTP.Disconnect;
            if (fIdSMTP.IOHandler <> Nil) then
              begin
                fIdSMTP.IOHandler.InputBuffer.Clear;
                fIdSMTP.IOHandler.Close;
              end;
          end;
      end;
    except
      on E:Exception do
        begin
          fErrorMessage:=E.Message;
          fIdSMTP.Disconnect(False);
          if (fIdSMTP.IOHandler <> Nil) then
            fIdSMTP.IOHandler.InputBuffer.Clear;
        end;
    end;
  finally
    pSendingResult:=Sent;
  end;
end;

Upvotes: 0

Views: 57

Answers (0)

Related Questions