unit EmailUtils;
interface
uses
IdSMTP, IdMessage, IdEMailAddress, IdAttachmentFile,
IdSSL, IdSSLOpenSSL, IdExplicitTLSClientServerBase, IdIOHandlerStack,
IdSMTPBase, IdTCPClient, IdIOHandler, IdIOHandlerSocket,
System.Classes, System.UITypes;
type
TEMailUtils =
class(TObject)
public
class function sendEmail(receiver,subject,body,attachment:
String;IdIOHandler:TIdSSLIOHandlerSocketOpenSSL):boolean;
class function sendHTMLEmail(receiver,subject,body,attachment:
String;IdIOHandler:TIdSSLIOHandlerSocketOpenSSL):boolean;
end;
implementation
uses
{$IFDEF IOS}
IdSSLOpenSSLHeaders_Static,
{$ENDIF}
System.SysUtils;
class function TEMailUtils.sendEmail(receiver,subject,body,attachment:
String;IdIOHandler:TIdSSLIOHandlerSocketOpenSSL):boolean;
var IDSMTP1:TIDSMTP;
IdMessage: TIdMessage;
errorInfo:
String;
begin
Result:=false;
try
IDSMTP1:=TIDSMTP.Create();
try
errorInfo:='
fill IDSMTP1';
IDSMTP1.Host := '
xyz';
IDSMTP1.Port := 123;
IDSMTP1.IOHandler := IdIOHandler;
IDSMTP1.AuthType := satDefault;
IDSMTP1.UseTLS := utUseRequireTLS;
IDSMTP1.Username := '
xyz';
IDSMTP1.Password := '
xyz';
IdMessage := TIdMessage.Create(
nil);
try
errorInfo:='
fill IdMessage';
IdMessage.From.
Name:='
xyz';
IdMessage.From.Address:='
xyz';
IdMessage.Subject:=subject;
IdMessage.Body.Add(body);
IdMessage.Recipients.EMailAddresses:=receiver;
if (attachment<>'
')
then
TIdAttachmentFile.Create(IdMessage.MessageParts,attachment);
errorInfo:='
check IDSMTP1';
if not IDSMTP1.Connected
then
begin
errorInfo:='
connect IDSMTP1';
IDSMTP1.Connect;
end;
errorInfo:='
send IdMessage';
IDSMTP1.Send(IdMessage);
Result:=true;
finally
IdMessage.Free;
end;
finally
IDSMTP1.Free;
IdIOHandler.Free;
end;
except on E:
Exception do
log.d('
Wrong E-Mail-Setup/Config: '+errorInfo+'
/'+e.
Message);
end;
end;
class function TEMailUtils.sendHTMLEmail(receiver,subject,body,attachment:
String;IdIOHandler:TIdSSLIOHandlerSocketOpenSSL):boolean;
var IDSMTP1:TIDSMTP;
IdMessage: TIdMessage;
errorInfo:
String;
begin
Result:=false;
try
IDSMTP1:=TIDSMTP.Create();
try
errorInfo:='
fill IDSMTP1';
IDSMTP1.Host := '
xyz';
IDSMTP1.Port := 123;
IDSMTP1.IOHandler := IdIOHandler;
IDSMTP1.AuthType := satDefault;
IDSMTP1.UseTLS := utUseRequireTLS;
IDSMTP1.Username := '
xyz';
IDSMTP1.Password := '
xyz';
IdMessage := TIdMessage.Create(
nil);
try
errorInfo:='
fill IdMessage';
IdMessage.From.
Name:='
xyz';
IdMessage.From.Address:='
xyz';
IdMessage.Subject:=subject;
IdMessage.ContentType:='
text/html';
IdMessage.Body.Add(body);
IdMessage.Recipients.EMailAddresses:=receiver;
if (attachment<>'
')
then
TIdAttachmentFile.Create(IdMessage.MessageParts,attachment);
errorInfo:='
check IDSMTP1';
if not IDSMTP1.Connected
then
begin
errorInfo:='
connect IDSMTP1';
IDSMTP1.Connect;
end;
errorInfo:='
send IdMessage';
IDSMTP1.Send(IdMessage);
Result:=true;
finally
IdMessage.Free;
end;
finally
IDSMTP1.Free;
IdIOHandler.Free;
end;
except on E:
Exception do
log.d('
Wrong E-Mail-Setup/Config: '+errorInfo+'
/'+e.
Message);
end;
end;
end.