How to send text formatted with Tidmessage smtp and richedit

Asked

Viewed 816 times

3

Hello. I am trying to send an email with formatted text using my Delphi program only instead of sending a text a file is sent. trx as an attachment.
I’m using: Tidmessage -> Idmsgsend // tidSMTP // Richedit

the sending 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 format

Imagem Fihcheiro .trx

I’ve tried things like loading a file to do direct assignments and stream pass. The end result is the same. Instead of formatted text, a . trx file with the formatting is attached. Something I’m missing? Thank you.

  • From what I’ve researched, the way is to send as HTML. Ever tried it? You’d have to convert the RTF for HTML and then send, the ContentType should be amended to text/html.

  • This means formatting text between tags?

  • Friend, see the following links: Soen. This link I did not test, but by his acceptance in Soen, seems to be interesting. Link refers to my first comment, see how it is first saved in Word, to convert to HTML and then sent by email. It’s kind of funny, but it might work. , RTF only in the Microsoft Outlook (or similar), the rest has to be HTML.

  • Thank you very much! The first option had already tested but it has the same behavior. I will now try to make the conversion to html and see how it behaves. As soon as I find some solution I put here. this is found....

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;

0

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

  • I’ve been to that website. But thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.