From be2f109936162bceb2109455bc372c0523df51ea Mon Sep 17 00:00:00 2001 From: Julio Senha Date: Thu, 18 Feb 2021 17:30:20 -0300 Subject: [PATCH] Update Mail4Delphi.pas --- src/Mail4Delphi.pas | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Mail4Delphi.pas b/src/Mail4Delphi.pas index b978835..5b127d9 100644 --- a/src/Mail4Delphi.pas +++ b/src/Mail4Delphi.pas @@ -315,7 +315,8 @@ destructor TMail.Destroy; function TMail.Disconnect: Boolean; begin - FIdSMTP.Disconnect; + if FIdSMTP.Connected then + FIdSMTP.Disconnect; UnLoadOpenSSLLibrary; Result := True; end; @@ -326,11 +327,13 @@ class function TMail.New: IMail; end; function TMail.SendMail: Boolean; +var + LImplicitConnection: Boolean; begin if not SetUpEmail then raise Exception.Create('Incomplete data!'); if not FIdSMTP.Connected then - Self.Connect; + LImplicitConnection := Self.Connect; try try FIdSMTP.Send(FIdMessage); @@ -340,7 +343,7 @@ function TMail.SendMail: Boolean; raise Exception.Create('Error sending message: ' + E.Message); end; finally - if FIdSMTP.Connected then + if LImplicitConnection then Self.Disconnect; end; end;