function TSSRemoteServer.SendMailMessage(Subject: string; BodyText, AttachedFiles: TStrings;): boolean;
var
IdSendMail: TIdSMTP;
IdMessage: TIdMessage;
SSLHandlerSmtp: TIdSSLIOHandlerSocketOpenSSL;
...
begin
Result := false;
IdSendMail := TIdSMTP.Create(NIL);
IdMessage := TIdMessage.Create(NIL);
SSLHandlerSmtp := TIdSSLIOHandlerSocketOpenSSL.Create(NIL);
try
try
IdSendMail.Host := FEmailServer;
IdSendMail.Port := FEmailPort; //Added 2021-05-05
if FEmailAuth then
begin
IdSendMail.AuthType := IdSmtp.satDefault;
IdSendMail.UserName := FEmailUser;
IdSendMail.Password := FEmailPwd;
end
else
begin
IdSendMail.AuthType := IdSmtp.satNone;
IdSendMail.UserName := '';
IdSendMail.Password := '';
end;
if FEmailSSL then //Added 2021-05-05 to handle the encrypted email send
begin
IdSendMail.IOHandler := SSLHandlerSmtp;
IdSendMail.UseTLS := utUseImplicitTLS;
SSLHandlerSmtp.Port := FEmailPort;
end;
IdSendMail.MailAgent := 'Remote Server';
IdMessage.IsEncoded := true;
IdMessage.Subject := Subject;
IdMessage.From.Text := FEmailSender;
IdMessage.Body.Text := BodyText.Text;
if AttachedFiles.Count > 0 then
begin
LogStd('Attachment count: ' + IntToStr(AttachedFiles.Count));
{Attach each file in the list to the message}
for i := 0 to AttachedFiles.Count -1 do
begin
sTmp := AttachedFiles[i];
if FileExists(sTmp) then
with TIdAttachmentFile.Create(IdMessage.MessageParts, sTmp) do
begin
ContentType := 'application/x-zip-compressed';
end;
end;
end;
{Now send}
LogStd('Message prepared, now send mail');
try
bSendOK := false;
IdSendMail.Connect;
if IdSendMail.Connected then
begin
try
IdSendMail.Send(IdMessage);
bSendOK := true;
finally
IdSendMail.Disconnect;
end;
if bSendOK then
begin
Result := true;
LogStd('Mail message sent. Subject: ' + Subject);
end;
end;
except
on E: Exception do
LogErr('Exception during email send: ' + E.Message);
end;
except
on E: Exception do
LogErr('Exception in TSSRemoteServer.SendMailMessage: ' + E.Message);
end;
finally
IdSendMail.Free;
IdMessage.Free;
SSLHandlerSmtp.Free;
end;
end;