Enviar PDF para e-mail automático

11/04/2019

12

Já tô sem saber o que fazer, tô tentando enviar email automatico quando chamar o form, mas ele faz todo o processo e não envia o e-mail.

procedure EnviarEmail(username, password, totarget, subject, body: string);
var
DATA: TIdMessage;
SMTP: TIdSMTP;
SSL: TIdSSLIOHandlerSocketOpenSSL;
begin

SMTP := TIdSMTP.Create(nil);
DATA := TIdMessage.Create(nil);
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);

SSL.SSLOptions.Method := sslvTLSv1;
SSL.SSLOptions.Mode := sslmUnassigned;
SSL.SSLOptions.VerifyMode := [];
SSL.SSLOptions.VerifyDepth := 0;

DATA.From.Address := username;
DATA.Recipients.EMailAddresses := totarget;
DATA.subject := subject;
DATA.body.text := body;

SMTP.IOHandler := SSL;
SMTP.Host := ''smtp.gmail.com'';
SMTP.Port := 587;
SMTP.username := username;
SMTP.password := password;
SMTP.UseTLS := utUseExplicitTLS;

SMTP.Connect;
SMTP.Send(DATA);
SMTP.Disconnect;

SMTP.Free;
DATA.Free;
SSL.Free;

end;

procedure TFRelProduto.DadosEMail;
begin
mail_username := ''gilson.oliver13@gmail.com'';
mail_password := ''sextafeira13'';
mail_to := ''gilson.oliver13@gmail.com'';
mail_subject := ''Relatório Produtos'';
mail_body := ''Relatório geral'' ;

try
begin
EnviarEmail(mail_username, mail_password, mail_to, mail_subject, mail_body);
end;
except
// Delphi Is mY Power?
end;

end;

Responder

Posts

18/04/2019

Fernando Wahl

Boa tarde Gilson,

Cara eu tenho uma parada aqui que peguei da internet uma vez e está funcionando, da uma olhada:

function TfrmPrincipal.SenEmail: boolean;
var
FileName : String;
begin
try
Result := False;

//Setup SMTP
//IdSMTP := TIdSMTP.Create(nil);
//IdMessage:=tIdMessage.Create(nil);
//IdSSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);



IdSMTP.IOHandler := IdSSLIOHandlerSocketOpenSSL;
IdSSLIOHandlerSocketOpenSSL.SSLOptions.Method := sslvSSLv2;
IdSMTP.UseTLS := utUseImplicitTLS;

{IdSSLIOHandlerSocketOpenSSL.MaxLineAction := maException;
IdSSLIOHandlerSocketOpenSSL.SSLOptions.Method := sslvSSLv23;
IdSSLIOHandlerSocketOpenSSL.SSLOptions.Mode := sslmUnassigned;
IdSSLIOHandlerSocketOpenSSL.SSLOptions.VerifyMode := [];
IdSSLIOHandlerSocketOpenSSL.SSLOptions.VerifyDepth := 0; }

IdSMTP.IOHandler := IdSSLIOHandlerSocketOpenSSL;
IdSMTP.Host := 'smtp.live.com';
IdSMTP.Port := 587;
IdSMTP.Username:='xxxxx@hotmail.com';
IdSMTP.Password:='xxxxxx';
IdSMTP.UseTLS := utUseImplicitTLS;

IdMessage.From.Address := 'xxxxx@hotmail.com';
IdMessage.Recipients.EMailAddresses := 'xxxx@live.com';
IdMessage.Subject := 'Relatório '+copy(datetostr(now),1,2)+copy(datetostr(now),4,2)+copy(datetostr(now),7,4);


FileName := 'C:\\arquivo.rar';

if FileExists(FileName) then
TIdAttachmentFile.Create(IdMessage.MessageParts, FileName);
//Send email
try
try
IdSMTP.Connect;
IdSMTP.Send(IdMessage);
Result := True;
IdSMTP.Disconnect;
except
on E:Exception do
begin
ShowMessage('Cannot send E-Mail: ' + E.Message);
Result := False;
end;
end;
finally
if IdSMTP.Connected then IdSMTP.Disconnect;
end;
except
on E : Exception do
begin
ShowMessage('Erro ao enviar relatório');
Result := False;
end;
end;
end;


Qualquer coisa da um grito. Valeu.
Responder