unit USMTPSend;
interface
uses
Windows, Messages, Classes, SysUtils, IdMessage;
type
TBodyFormat = (bfNone, bfPlain, bfRTF, bfHTML);
function SendEmail(AHost: string; APort: Word; AUserName: string; APassword: string;
const AMessage: TIdMessage; AUseTLS: Boolean = True): Boolean; overload;
function SendEmail(AHost: string; APort: Word; AUserName: string; APassword: string;
AFrom: string;
ARecipients: string; ACCRecipients: string; ABCCRecipients: string;
ASubject: string;
const AMessageBody: TStrings; ABodyFormat: TBodyFormat = bfPlain;
const AAttachments: TStrings = nil): Boolean; overload;
function SendEmail(AHost: string; APort: Word; AUserName: string; APassword: string;
AFrom: string; ARecipients: string; ASubject: string;
const AMessageBody: TStrings; ABodyFormat: TBodyFormat = bfPlain;
const AAttachments: TStrings = nil): Boolean; overload;
implementation
uses
IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdText,
IdAttachmentFile, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdExplicitTLSClientServerBase, IdMessageClient, IdSMTPBase, IdSMTP;
function SendEmail(AHost: string; APort: Word; AUserName: string; APassword: string;
const AMessage: TIdMessage; AUseTLS: Boolean = True): Boolean;
var
oSMTP: TIdSMTP;
begin
oSMTP := TIdSMTP.Create(nil);
try
oSMTP.Host := AHost;
oSMTP.Port := APort;
oSMTP.Username := AUserName;
oSMTP.Password := APassword;
if AUseTLS then
begin
oSMTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(oSMTP);
TIdSSLIOHandlerSocketOpenSSL(oSMTP.IOHandler).SSLOptions.Method := TIdSSLVersion(sslvTLSv1_2);
oSMTP.UseTLS := TIdUseTLS.utUseExplicitTLS;
end
else
oSMTP.UseTLS := utNoTLSSupport;
try
oSMTP.Connect;
try
if AUseTLS then
oSMTP.Authenticate;
oSMTP.Send(AMessage);
Result := True;
finally
oSMTP.Disconnect;
end;
except
// on E: EIdReplyRFCError do
on E: Exception do
begin
// LogException(E);
Exit(False);
end;
end;
finally
oSMTP.Free;
end;
end;
function SendEmail(AHost: string; APort: Word; AUserName: string; APassword: string;
AFrom: string;
ARecipients: string; ACCRecipients: string; ABCCRecipients: string;
ASubject: string;
const AMessageBody: TStrings; ABodyFormat: TBodyFormat = bfPlain;
const AAttachments: TStrings = nil): Boolean;
var
oMessage: TIdMessage;
oText: TIdText;
iIndex: Integer;
begin
oMessage := TIdMessage.Create(nil);
try
oMessage.From.Text := AFrom;
oMessage.Recipients.EMailAddresses := ARecipients;
oMessage.CCList.EMailAddresses := ACCRecipients;
oMessage.BCCList.EMailAddresses := ABCCRecipients;
oMessage.Subject := ASubject;
if ABodyFormat = bfPlain then
oMessage.Body.Assign(AMessageBody)
else
begin
oMessage.Body.Clear;
oText := TIdText.Create(oMessage.MessageParts, AMessageBody);
case ABodyFormat of
bfHTML: oText.ContentType := 'text/html; charset=iso-8859-1';
bfRTF: oText.ContentType := 'text/enriched';
else
oText.ContentType := 'text/plain';
end;
end;
if Assigned(AAttachments) and
(AAttachments.Count > 0) then
for iIndex := 0 to Pred(AAttachments.Count) do
if FileExists(AAttachments[iIndex]) then
TIdAttachmentFile.Create(oMessage.MessageParts, AAttachments[iIndex]);
Result := SendEMail(AHost, APort, AUserName, APassword, oMessage);
finally
oMessage.Free;
end;
end;
function SendEmail(AHost: string; APort: Word; AUserName: string; APassword: string;
AFrom: string; ARecipients: string; ASubject: string;
const AMessageBody: TStrings; ABodyFormat: TBodyFormat = bfPlain;
const AAttachments: TStrings = nil): Boolean;
begin
Result := SendEmail(AHost, APort, AUserName, APassword,
AFrom, ARecipients, '', '', ASubject,
AMessageBody, ABodyFormat, AAttachments);
end;
end.
Bookmarks