GARANTIR DESCONTO

Fórum Envio de e-mail com delphi 2010 e indy 10 com imagem no corpo como assinatura #513191

10/03/2015

0

Amigos, bom dia!

Já pesquisei no fórum e tudo que encontrei está como eu estou fazendo. Acredito que meu problema está sendo gerado pela versão do indy, pois a maioria dos post o exemplo é do 9 e eu estou usando o 10.
Tenho uma função que envia e-mail e preciso enviar a assinatura do e-mail como uma imagem. Já fiz a referencia da imagem no html e anexei a mesma, porem ela não aparece.

Abaixo segue a minha função, desde já agradeço.

procedure Enviar_E_Mail(A_IdSMTP: TIdSMTP; sUserEmail, sPassWord, sHost, sPorta, sAssunto,
sFileCorpo: string; bTLS, bAuth: boolean; sDestinatarios, sCC, sCCo,
sTextoCorpo, sAnexo: TStringList; bCopiaParaOEmitente: boolean);
var
i : Smallint;
A_IdSSLIOHandler : TIdSSLIOHandlerSocketOpenSSL;
A_IdMessage : TIdMessage;
anexo :TIdAttachmentFile;
A_Texto : TStringList;
s : string;
begin
A_IdSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create();
A_IdMessage := TIdMessage.Create();

with A_IdSMTP do
begin
Disconnect;
ConnectTimeout := 10000;
ReadTimeout := 10000;
Host := sHost;
Port := StrToInt(sPorta);
Username := sUserEmail;
Password := sPassWord;

if bAuth then
begin
AuthType := satDefault
end
else
begin
AuthType := satNone;
end;

if bTLS then
begin
A_IdSSLIOHandler.SSLOptions.Method := sslvTLSv1;
A_IdSSLIOHandler.SSLOptions.Mode := sslmClient;
A_IdSSLIOHandler.PassThrough := true;
IOHandler := A_IdSSLIOHandler;
//UseTLS := utUseImplicitTLS;
UseTLS := utUseExplicitTLS;
end
else
begin
IOHandler := nil;
UseTLS := utNoTLSSupport;
end;
end;

with A_IdMessage do
begin
Clear;
Body.Clear;
Recipients.Clear;
From.Address := sUserEmail;
From.Name := VS_NomeUsuLogado;
ReceiptRecipient.Address := sUserEmail;
Subject := sAssunto;
ContentType := 'multipart/mixed';
end;

s := '';

for i := 0 to sDestinatarios.Count - 1 do
begin
s := s + sDestinatarios[i];

If i < sDestinatarios.Count-1 then
begin
s := s + ';';
end;
end;

if bCopiaParaOEmitente then
begin
s := s + ';' + sUserEmail;
end;

A_IdMessage.Recipients.EMailAddresses := s;

if sCC <> nil then
begin
s := '';
for i := 0 to sCC.Count - 1 do
begin
s := s + sCC[i];

if i < sCC.Count-1 then
begin
s := s + ';';
end;
end;

A_IdMessage.CCList.EMailAddresses := s;
end;

if sCCo <> nil then
begin
s := '';
for i := 0 to sCCo.Count - 1 do
begin
s := s + sCCo[i];

if i < sCCo.Count-1 then
begin
s := s + ';';
end;
end;

A_IdMessage.BccList.EMailAddresses := s;
end;

if ((Trim(sFileCorpo) <> '') and FileExists(sFileCorpo)) or
(Trim(sTextoCorpo.Text) <> '') then
begin
with TIdText.Create(A_IdMessage.MessageParts) do
begin
ContentType := 'text/html';
ContentTransfer := '7bit';
CharSet := 'ISO-8859-1';
Body.Append('<html><head>');
Body.Append('<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">');
Body.Append('</head><body>');
Body.Append('<table>');
Body.Append('<tr>');
Body.Append('<td>');

for i := 0 to sTextoCorpo.Count - 1 do
begin
Body.Append(sTextoCorpo[i] + '<br>');
end;

Body.Append('</td>');
Body.Append('</tr>');

Body.Append('<tr>');
Body.Append('<td>');

if (Trim(sFileCorpo) <> '') and FileExists(sFileCorpo) then
begin
Body.Append('<img src="cid:assinatura.jpg" />');
end;

Body.Append('</td>');
Body.Append('</tr>');
Body.Append('</table>');
Body.Append('</body></html>');

end;

if ((Trim(sFileCorpo) <> '') and FileExists(sFileCorpo)) then
begin
anexo := TIdAttachmentFile.Create(A_IdMessage.MessageParts,sFileCorpo);
anexo.ContentType := 'image/jpeg';
anexo.ContentDisposition := 'inline';
anexo.Headers.Add('Content-ID: <assinatura.jpg>');
end;
end;

if sAnexo <> Nil then
begin
for i := 0 to sAnexo.Count - 1 do
begin
if FileExists(TFileName(sAnexo[i])) then
begin
with TIdAttachmentFile.Create(A_IdMessage.MessageParts, TFileName(sAnexo[i])) do
begin
//ContentType := 'application/octet-stream';
ContentDisposition := 'attachment';
ContentTransfer := 'base64';
end;
end;
end;
end;

try
A_IdSMTP.Connect;
A_IdSMTP.Authenticate;
A_IdSMTP.Send(A_IdMessage);
A_IdSMTP.Disconnect;

Alerta('M', 'Envio de E-Mail', 'Mensagem enviada com sucesso.', Nil, ['&Ok']);
except
On E:Exception do
begin
Alerta('E', 'Envio de E-Mail', '', E, ['&Ok']);
A_IdSMTP.Disconnect;
end;
end;

FreeAndNil(A_IdMessage);
FreeAndNil(A_IdSSLIOHandler);


{Os arquivos que devem acompanhar a aplicação são:
libssl32.dll
ssleay32.dll
libeay32.dll : esse não tem a ver com a função, mas a unit tem coisas do ACBr e portanto...
openssl.exe : acho que esse ajuda um pouco na "mágica" de funcionar com o gmail

Segue link para o openssl.exe: http://terraskilll.net23.net/openssl.zip

Geralmente os deixamos na pasta do executável, não em System32.

Para o caso do Gmail, geralmente marcamos as opções TLS e Autenticar. Há também uma alteração para o hotmail, obrigando a usar o SSL. Testamos com alguns dos principais e-mails (uol, terra, gmail, yahoo, bol) e até com servidores particulares (de alguns clientes). Até o momento, tem funcionado bem.}
end;
Haroldo Bordignon

Haroldo Bordignon

Responder

Posts

12/03/2015

Haroldo Bordignon

Amigos, ninguém sabe me ajudar, estou precisando muito disso...
Responder

Gostei + 0

12/03/2015

Haroldo Bordignon

Amigos, acabei conseguindo resolver sozinho...

Segue abaixo a função já alterada, agora a imagem da assinatura do cliente aparece perfeitamente.

Abraço.

procedure Enviar_E_Mail(A_IdSMTP: TIdSMTP; sUserEmail, sPassWord, sHost, sPorta, sAssunto,
sFileCorpo: string; bTLS, bAuth: boolean; sDestinatarios, sCC, sCCo,
sTextoCorpo, sAnexo: TStringList; bCopiaParaOEmitente: boolean);
var
i : Smallint;
A_IdSSLIOHandler : TIdSSLIOHandlerSocketOpenSSL;
A_IdMessage : TIdMessage;
bldr: TIdMessageBuilderHtml;
s : string;
begin
A_IdSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(Nil);
A_IdMessage := TIdMessage.Create(Nil);

with A_IdSMTP do
begin
Disconnect;
ConnectTimeout := 10000;
ReadTimeout := 10000;
Host := sHost;
Port := StrToInt(sPorta);
Username := sUserEmail;
Password := sPassWord;

if bAuth then
begin
AuthType := satDefault
end
else
begin
AuthType := satNone;
end;

if bTLS then
begin
A_IdSSLIOHandler.SSLOptions.Method := sslvTLSv1;
A_IdSSLIOHandler.SSLOptions.Mode := sslmClient;
A_IdSSLIOHandler.PassThrough := true;
IOHandler := A_IdSSLIOHandler;
//UseTLS := utUseImplicitTLS;
UseTLS := utUseExplicitTLS;
end
else
begin
IOHandler := nil;
UseTLS := utNoTLSSupport;
end;
end;

with A_IdMessage do
begin
Clear;
Body.Clear;
Recipients.Clear;
From.Address := sUserEmail;
From.Name := VS_NomeUsuLogado;
ReceiptRecipient.Address := sUserEmail;
Subject := sAssunto;
ContentType := 'multipart/mixed';
end;

s := '';

for i := 0 to sDestinatarios.Count - 1 do
begin
s := s + sDestinatarios[i];

If i < sDestinatarios.Count-1 then
begin
s := s + ';';
end;
end;

if bCopiaParaOEmitente then
begin
s := s + ';' + sUserEmail;
end;

A_IdMessage.Recipients.EMailAddresses := s;

if sCC <> nil then
begin
s := '';
for i := 0 to sCC.Count - 1 do
begin
s := s + sCC[i];

if i < sCC.Count-1 then
begin
s := s + ';';
end;
end;

A_IdMessage.CCList.EMailAddresses := s;
end;

if sCCo <> nil then
begin
s := '';
for i := 0 to sCCo.Count - 1 do
begin
s := s + sCCo[i];

if i < sCCo.Count-1 then
begin
s := s + ';';
end;
end;

A_IdMessage.BccList.EMailAddresses := s;
end;

if ((Trim(sFileCorpo) <> '') and FileExists(sFileCorpo)) or
(Trim(sTextoCorpo.Text) <> '') then
begin
bldr := TIdMessageBuilderHtml.Create;
bldr.HtmlCharSet := 'ISO-8859-1';
try
bldr.Html.Add('<html>');
bldr.Html.Add('<head>');
bldr.Html.Add('<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">');
bldr.Html.Add('</head>');
bldr.Html.Add('<body>');
bldr.Html.Add('<table>');
bldr.Html.Add('<tr>');
bldr.Html.Add('<td>');

for i := 0 to sTextoCorpo.Count - 1 do
begin
bldr.Html.Add(sTextoCorpo[i] + '<br>');
end;

bldr.Html.Add('</td>');
bldr.Html.Add('</tr>');

bldr.Html.Add('<tr>');
bldr.Html.Add('<td>');

if (Trim(sFileCorpo) <> '') and FileExists(sFileCorpo) then
begin
bldr.Html.Add('<img src="cid:assinatura" />');
end;

bldr.Html.Add('</td>');
bldr.Html.Add('</tr>');
bldr.Html.Add('</table>');

bldr.Html.Add('</body>');
bldr.Html.Add('</html>');

if (Trim(sFileCorpo) <> '') and FileExists(sFileCorpo) then
begin
bldr.HtmlFiles.Add(sFileCorpo, 'assinatura');
end;

bldr.FillMessage(A_IdMessage);
finally
freeandnil(bldr);
end;
end;

if sAnexo <> Nil then
begin
for i := 0 to sAnexo.Count - 1 do
begin
if FileExists(TFileName(sAnexo[i])) then
begin
with TIdAttachmentFile.Create(A_IdMessage.MessageParts, TFileName(sAnexo[i])) do
begin
ContentDisposition := 'attachment';
ContentTransfer := 'base64';
end;
end;
end;
end;

try
A_IdSMTP.Connect;
A_IdSMTP.Authenticate;
A_IdSMTP.Send(A_IdMessage);
A_IdSMTP.Disconnect;

Alerta('M', 'Envio de E-Mail', 'Mensagem enviada com sucesso.', Nil, ['&Ok']);
except
On E:Exception do
begin
Alerta('E', 'Envio de E-Mail', '', E, ['&Ok']);
A_IdSMTP.Disconnect;
end;
end;

FreeAndNil(A_IdMessage);
FreeAndNil(A_IdSSLIOHandler);


{Os arquivos que devem acompanhar a aplicação são:
libssl32.dll
ssleay32.dll
libeay32.dll : esse não tem a ver com a função, mas a unit tem coisas do ACBr e portanto...
openssl.exe : acho que esse ajuda um pouco na "mágica" de funcionar com o gmail

Segue link para o openssl.exe: http://terraskilll.net23.net/openssl.zip

Geralmente os deixamos na pasta do executável, não em System32.

Para o caso do Gmail, geralmente marcamos as opções TLS e Autenticar. Há também uma alteração para o hotmail, obrigando a usar o SSL. Testamos com alguns dos principais e-mails (uol, terra, gmail, yahoo, bol) e até com servidores particulares (de alguns clientes). Até o momento, tem funcionado bem.}
end;
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar