function MailSend(Const MailFrom,Replyto,SMTPHost,SMTPPort,user,password,MailTo, MailSubject,MailBody,Mailattachments,cripto: string): Boolean;
var
SMTP: TSMTPSend;
sl:TStringList;
mime:TMimemess;
part: TMimepart;
x:integer;
anexo:TstringList;
error:string;
i,l:integer;
temp,MyMailTo,MailToSend:String;
mailSeparator:Char;
begin
MailToSend:='';
i:=1;
MyMailTo:=trim(MailTo);
l:=Length(MyMailTo);
mailSeparator:=',';
while i<=l do begin
if MyMailTo[i] in [#1..#32,':',';','|',','] then begin
if (i>1) and (not(copy(MailToSend,length(MailToSend),1)=mailSeparator)) then
MailToSend:=MailToSend+mailSeparator;
end else
MailToSend:=MailToSend+MyMailTo[i];
inc(i);
end;
error:='';
Result:=False;
SMTP:=TSMTPSend.Create;
sl:=TStringList.Create;
mime:=TMimemess.create;
mime.Header.CharsetCode:=UTF_8;
sl.text:= SysToUTF8(MailBody);
part := mime.AddPartMultipart('mixed', nil);
mime.AddPartTextEx(sl,part,UTF_8,false,ME_BASE64);
Anexo:=TStringList.create;
anexo.text:=Mailattachments;
if anexo.Count>0 then begin
for x:=0 to anexo.count-1 do
mime.AddPartBinaryFromFile(anexo[x],part);
anexo.free;
end;
mime.header.subject:=SysToUTF8(MailSubject);
mime.header.from:=MailFrom;
mime.header.ReplyTo:=Replyto;
mime.Header.XMailer:=pb_SystemName;
mime.Header.Organization:='';
mime.EncodeMessage;
SMTP.UserName:=User;
SMTP.Password:=Password;
SMTP.TargetHost:=SMTPHost;
SMTP.AutoTLS:=cripto='TLS';
SMTP.FullSSL:=cripto='SSL';
SMTP.TargetPort:=SMTPPort;
if SMTP.Login then begin
result:=SMTP.MailFrom(Replyto, Length(mime.Lines.Text));
if result then begin
while result and (length(MailToSend)>0) do begin
temp:=trim(ExtractStrUtf8(MailToSend,mailSeparator));
if length(temp)>0 then
result:=SMTP.MailTo(temp);
end;
if result then begin
result:=SMTP.MailData(mime.lines);
if not result then
error:='MailData Error: '+smtp.ResultString;
end else error:='MailTo Error: '+smtp.ResultString;
end else error:='MailFrom Error: '+smtp.ResultString;
SMTP.Logout;
end else error:='Login Error: '+smtp.ResultString;
SMTP.Free;
sl.Free;
mime.free;
if (not result) and (length(trim(error))>0) then
raise Exception.Create(error)
else
DebugLn('Email','"'+MailSubject+'" enviado para "'+MailToSend+'".');
end;