PAGUE 6 MESES
LEVE 12 MESES
GARANTIR DESCONTO

Fórum Delphi: HTML não é exibido no corpo do e-mail em alguns programas de e-mail. #478322

08/05/2014

0

Pessoal, tenho um código que envia e-mails com formatação HTML e anexos. Funciona muito bem quando o destinatário lê o e-mail no MS Outlook ou no EMClient, por exemplo. Porém em alguns programas o corpo do e-mail fica em branco, sem exibir o HTML. Um mesmo e-mail que visualizo com sucesso no MS Outlook fica vazio no GMAIL (IE/Chrome).

Alguém tem uma dica? Não achei nada nas pesquisas que fiz. Segue meu código, utilizando o Indy.

var
  oHtml   : TIdText; //Corpo do E-Mail (HTML)
  oText   : TIdText; //Anexos (Texto)
  oAnexos : TStringList;
  iIdx    : Integer;
begin
  if (AllTrim(fConfigEMail.Values['LOGIN']) = EmptyStr) or
     (AllTrim(fConfigEMail.Values['SENHA']) = EmptyStr) or
     (AllTrim(fConfigEMail.Values['HOST']) = EmptyStr) or
     (AllTrim(fConfigEMail.Values['PORTA']) = EmptyStr) or
     (AllTrim(fConfigEMail.Values['SSL_TLS']) = EmptyStr) then
    raise Exception.Create('Preencha as configurações de e-mail nos parâmetros do sistema.');

  fIdSMTP.Username  := fConfigEMail.Values['LOGIN'];
  fIdSMTP.Password  := fConfigEMail.Values['SENHA'];
  fIdSMTP.Host      := fConfigEMail.Values['HOST'];
  fIdSMTP.Port      := StrToInt(fConfigEMail.Values['PORTA']);
  if fConfigEMail.Values['SSL_TLS'] <> 'NDA' then
    begin
      //envio com criptografia SSL ou TLS
      fIdSMTP.IOHandler := fIdSSLSocket;
      if fConfigEMail.Values['SSL_TLS'] = 'TLS' then
        fIdSSLSocket.SSLOptions.Method := sslvTLSv1
      else
        fIdSSLSocket.SSLOptions.Method := sslvSSLv2;
      fIdSSLSocket.SSLOptions.Mode     := sslmClient;
    end;
  fIdSMTP.AuthenticationType       := atLogin;
  fIdMessage.From.Address          := fConfigEMail.Values['NOREPLAY'];
  if AllTrim(fConfigEMail.Values['COPIA']) <> EmptyStr then
    fIdMessage.CCList.EMailAddresses := fConfigEMail.Values['COPIA'];
  fIdMessage.Priority              := mpNormal;
  fIdMessage.MessageParts.Clear;
  fIdMessage.Recipients.EMailAddresses := fEMailDestino;
  fIdMessage.Subject                   := fAssunto;
  
  //possibilita envio de e-mail com HTML e anexos
  fIdMessage.ContentType := 'multipart/mixed'; 

  fIdMessage.ContentTransferEncoding := '8bit';
  fIdMessage.CharSet := 'ISO-8859-1';

  oHtml := TIdText.Create(fIdMessage.MessageParts);
  try
    oHtml.ContentType := 'text/html';
    //Carregar corpo do e-mail em HTML
    oHtml.Body.Text   := fHTML;

    oText := TIdText.Create(fIdMessage.MessageParts);
    try
      oText.ContentType := 'text/plain';
      //Carregar anexos, se existirem.
      if AllTrim(fAnexos) <> EmptyStr then
        begin
          oAnexos := TStringList.Create;
          try
            oAnexos.Text := fAnexos;
            for iIdx := 0 to oAnexos.Count - 1 do
              begin
                if FileExists(oAnexos.Strings[iIdx]) then
                  TIdAttachment.Create(fIdMessage.MessageParts,oAnexos.Strings[iIdx]);
              end;
          finally
            oAnexos.Free;
          end;
        end;
      try
        fIdSMTP.Connect;
        try
          if fIdSMTP.Authenticate then
            fIdSMTP.Send(fIdMessage)
          else
            raise Exception.Create('Erro na autenticação do e-mail.');
        finally
          fIdSMTP.Disconnect;
        end;
      except
        on E : Exception do
          raise Exception.Create('Erro no envio do e-mail: ' + E.Message);
      end;
    finally
      oText.Free;
    end;
  finally
    oHtml.Free;
  end;
Carlo Ferreira

Carlo Ferreira

Responder

Posts

08/05/2014

Carlo Ferreira

Faltou acrescentar: no HOTMAIL consigo visualizar o HTML, porém com a acentuação errada.
Responder

Gostei + 0

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

Aceitar