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):boolean;
end;
implementation
{$IFDEF IOS}
uses System.SysUtils,IdSSLOpenSSLHeaders_Static;
{$ELSE}
uses System.SysUtils;
{$ENDIF}
class function TEMailUtils.sendEmail(receiver,subject,body,attachment:
String):boolean;
var IDSMTP1:TIDSMTP;
IdIOHandler:TIdIOHandler;
IdMessage: TIdMessage;
errorInfo:
String;
begin
Result:=false;
try
IDSMTP1:=TIDSMTP.Create();
IdIOHandler:=TIdSSLIOHandlerSocketOpenSSL.Create();
try
errorInfo:='
fill IDSMTP1';
IDSMTP1.Host := '
[smtp-server]';
IDSMTP1.Port := [Port];
IDSMTP1.IOHandler := IdIOHandler;
IDSMTP1.AuthType := satDefault;
IDSMTP1.UseTLS := utUseRequireTLS;
IDSMTP1.Username := '
[username]';
IDSMTP1.Password := '
[password]';
IdMessage := TIdMessage.Create(
nil);
try
errorInfo:='
fill IdMessage';
IdMessage.From.
Name:='
[From]';
IdMessage.From.Address:='
[From-Emailaddress]';
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:='
conect 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
//do something
end;
end;
end.