3

Olá. Estou a tentar mandar um e-mail com texto formatado a utilizar o meu programa delphi só que ao invés de ser enviado um texto é enviado um ficheiro .trx como anexo.
estou a utilizar: TidMEssage -> IdMsgSend // tidSMTP // RichEdit

o codigo de envio é seguinte:

with(TIdText.Create(IdMsgSend.MessageParts,nil))do
begin
   IdMsgSend.ContentType := 'text/RichEdit';
   RichEdit.Lines.SaveToFile('FileRTF.rtf');
   IdMsgSend.Body.LoadFromFile('FileRTF.rtf');
end;

...

a parte de envio é assim

    try
       SMTP.ConnectTimeout:=15000;
       SMTP.ReadTimeout:=15000;
       SMTP.Connect;
       SMTP.Send(IdMsgSend);
       Self.Tag:=1;
    except
    on E:Exception do
       ShowMessage(E.Message) ;
   end;

no ficheiro trx que vai em anexo tem esta formatação

Imagem Fihcheiro .trx

ja tentei coisas como ao invés de carregar de um ficheiro fazer atribuições diretas e passagem por stream. O resultado final é o mesmo. Ao inves de texto formatado é passado em anexo um ficheiro .trx com as formatações. Algo que me está a escapar? Obrigado.

4
  • Pelo que pesquisei, o jeito é enviar como HTML. Já tentou? Teria primeiro que converter o RTF para HTML e depois enviar, sendo que o ContentType deveria ser alterado para text/html.
    – Andrey
    29/11/2016 às 13:12
  • Isso significa formatar o texto entre tags? 29/11/2016 às 14:21
  • Amigo, veja os seguintes links: SOen. Esse link não testei, mas pela aceitação dele no SOen, parece ser interessante. Esse outro Link é referente meu primeiro comentário, veja como primeiro é salvo em Word, para converter para HTML e depois enviado por e-mail. É meio gambiarra, mas pode funcionar. Resumindo, RTF apenas no Microsoft Outlook (ou parecidos), o resto tem que ser HTML.
    – Andrey
    29/11/2016 às 15:46
  • Muito obrigado! A primeira opção já tinha testado mas ele tem o mesmo comportamento. Vou agora tentar fazer a conversão pra html e ver como ele se comporta. Assim que encontrar alguma solução eu coloco aqui. isso se encontrar.... 29/11/2016 às 16:23

2 Respostas 2

2

Essa função resolveu o meu problema visto que estava a exportar de uma RichEdit...

function TFEnviarMailDirecto.RtfToHtml(RICH: TcxRichEdit): string;
var
  I,J,max_array: integer;
  html: string;
  F,FO: array [0..5] of string;
  str,lgt: integer;
  size,breaklines: integer;
  cor : string;
begin
  html := '';
  str := rich.SelStart;
  lgt := rich.SelLength;
  MAX_ARRAY := 5;
  breaklines:=0;



 for J := 0 to MAX_ARRAY do
  begin
    F[J] := '';
    FO[J] := '';
  end;



for I := 0 to length(RICH.text) do
  begin
    RICH.SelStart := I-BREAKLINES;
    RICH.SelLength := 1;
for J := 0 to MAX_ARRAY do
  FO[J] := F[J];

if (RICH.SelAttributes.Style - [fsItalic] - [fsUnderline] - [fsStrikeOut] = [fsBold]) then
  F[0] := '<b>'
else
  F[0] := '</b>';

if (RICH.SelAttributes.Style - [fsItalic] - [fsBold] - [fsStrikeOut] = [fsUnderline]) then
  F[1] := '<u>'
else
  F[1] := '</u>';

if (RICH.SelAttributes.Style - [fsUnderline] - [fsBold] - [fsStrikeOut] = [fsItalic]) then
  F[2] := '<i>'
else
  F[2] := '</i>';

if RICH.SelAttributes.size < 10 then
  SIZE := 1
else if RICH.SelAttributes.size < 12 then
  SIZE := 2
else if RICH.SelAttributes.size < 14 then
  SIZE := 3
else if RICH.SelAttributes.size < 18 then
  SIZE := 4
else if RICH.SelAttributes.size < 22 then
  SIZE := 5
else if RICH.SelAttributes.size < 32 then
  SIZE := 6
else
  SIZE := 7;
COR := ColorToString(RICH.SelAttributes.Color);
if (COR = 'clWindowText') or (COR = 'clBlack') then
  COR := '#000000'
else if COR = 'clWite' then
  COR := '#FFFFFF'
else if COR = 'clAqua' then
  COR := '#00FFFF'
else if COR = 'clFuchsia' then
  COR := '#FF00FF'
else if COR = 'clBlue' then
  COR := '#0000FF'
else if COR = 'clYellow' then
  COR := '#FFFF00'
else if COR = 'clLime' then
  COR := '#00FF00'
else if COR = 'clRed' then
  COR := '#FF0000'
else if COR = 'clSilver' then
  COR := '#C0C0C0'
else if COR = 'clGray' then
  COR := '#808080'
else if COR = 'clTeal' then
  COR := '#008080'
else if COR = 'clPurple' then
  COR := '#800080'
else if COR = 'clNavy' then
  COR := '#000080'
else if COR = 'clOlive' then
  COR := '#808000'
else if COR = 'clGreen' then
  COR := '#008000'
else if COR = 'clMaroon' then
  COR := '#800000'
else if copy(COR,1,1) = '$' then
  COR := '#'+copy(COR,length(COR)-1,2)
            +copy(COR,length(COR)-3,2)
            +copy(COR,length(COR)-5,2)
else
  COR := '#000000';
F[3] := '</font><font face="'+RICH.SelAttributes.Name+'" size='+inttostr(SIZE)+' color="'+COR+'">';

if RICH.Paragraph.Alignment = taCenter then
  F[4] := '<center>'
else
  F[4] := '</center>';

if RICH.Paragraph.Alignment = taRightJustify then
  F[5] := '<div align="right">'
else
  F[5] := '</div>';

for J := 0 to MAX_ARRAY do
  if FO[J] <> F[J] then
    HTML := HTML + F[J];

if copy(RICH.text,I+1,1) = #13 then
begin
  inc(BREAKLINES); 
  HTML := HTML + '<br>';
end;

HTML := HTML + copy(RICH.text,I+1,1);
  end;
  RICH.SelStart := STR;
  RICH.SelLength := LGT;

  result := HTML;
end;
0

Olá, caso opte por formatar em HTML no corpo do e-mail, sugiro a leitura deste post, pois a forma como manipular os objetos do indy pode variar de acordo com o que você deseja enviar no e-mail (imagens no corpo do e-mail, imagens em anexo, etc).

1
  • Já estive nesse website. Mas obrigado. 30/11/2016 às 9:10

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .