function fnSendEMail(sHost, sUsername, sPassword, sFrom, sSenderEMail, sReceipt,
sSubject, sAttachment : String; strlBody : TStrings;
iPort : Integer) : Boolean;
var
idAttachment : TIdAttachmentFile;
idMessage : TIdMessage;
SMTP : TIdSMTP;
begin
Result := True;
//Create TIdSMTP and TIdMessage Componente
SMTP := TIdSMTP.Create();
idMessage := TIdMessage.Create();
//Configure SMPTP
SMTP.Host := sHost;
SMTP.Username := sUsername;
SMTP.Password := sPassword;
SMTP.Port := iPort;
//Configure idMessage
idMessage.From.Text := sFrom;
idMessage.Sender.Text := sSenderEMail;
idMessage.Recipients.EMailAddresses := sReceipt;
idMessage.Subject := sSubject;
idMessage.Body.Clear;
idMessage.Body := strlBody;
//Attach Files
If sAttachment <> EmptyStr Then
Begin
If FileExists(sAttachment) = True Then
Begin
idAttachment := TIdAttachmentFile.Create(idMessage.MessageParts, sAttachment);
End;
End;
//Send E-Mail
Try
Try
SMTP.Connect;
SMTP.Send(idMessage);
SMTP.Disconnect;
Except
Result := False;
If
SMTP.Connected = True Then
Begin
Try
SMTP.Disconnect;
Except
MessageDlg('Could not disconnect from the E-Mail Server: ' + sHost, mtError, [mbOK], 0);
End;
End;
MessageDlg('Could not send E-Mail to: ' + sReceipt, mtError, [mbOk], 0);
End;
Finally
//Destroy TIdSMTP and TIdMessage Componente
SMTP.Free;
idMessage.Free;
End;