How to send text formatted with TidMessage smtp and richedit

3

Hello. I am trying to send an email with formatted text to use my delphi program only that instead of being sent a text is sent a .trx file as attachment. I'm using: TidMEssage - > IdMsgSend // tidSMTP // RichEdit

the shipping code is as follows:

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

...

the shipping part is like this

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

in the attached trx file has this formatting

I've tried things like instead of loading a file to do direct assignments and streaming. The end result is the same. Instead of formatted text, a .trx file with the formatting is attached. Something that is escaping me? Thank you.

    
asked by anonymous 29.11.2016 / 13:47

2 answers

2

This function solved my problem since I was exporting from a 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;
    
30.11.2016 / 11:22
0

Hello, if you choose to format HTML in the body of the email, I suggest reading from this post , because the way you manipulate indy objects can vary according to what you want to send in the email (images in the body of the email, attached images, etc.).

    
29.11.2016 / 19:50