views:

127

answers:

2

I would like to perform the following task: converting a TRichEdit content (an rtf text) into a not-plain-text e-mail message body.

MAPI doesn't support rtf, but is there a way to do it maybe with Indy?

The problem is that rtf is rtf and emails are plain text or HTML.

Can someone suggest a trick? Is it possible to convert rtf to text using TWebBrowser?

Basically the scenario is:
1) User writes email in a delphi form,
2) The email is then sent with MAPI to the default mail client (so a new email window is generated, and the message body is the same I had in delphi form)
3) User sends the email from mail client

Anyway MAPI accepts only plain text.

UPDATE:

Trying with Indy I wrote this but still it doesn't work, as I send a mail it to my gmail account I recieve a message with empty body and NONAME fake attachment.

uses IdMessageBuilder;


procedure SendMail;
var
  MBuilder: TIdMessageBuilderRtf;
  MyMemoryStream: TMemoryStream;
begin
  try
    MBuilder := TIdMessageBuilderRtf.Create;
    MyMemoryStream := TMemoryStream.Create;
    MBuilder.RtfType := idMsgBldrRtfRichtext;
    // RichEdit1 has PlainText set to False
    // at design time I pasted some formatted text onto it
    RichEdit1.Lines.SaveToStream(MyMemoryStream);
    MBuilder.Rtf.LoadFromStream(MyMemoryStream);
    MBuilder.FillMessage(IdMessage1);
    IdSMTP1.Connect;
    IdSMTP1.Send(IdMessage1);
    IdSMTP1.Disconnect;
  finally
    MyMemoryStream.Free;
    MBuilder.Free;
  end;
end;
+1  A: 

Apparently (googled this myself, as I mail some from Delphi, but not in this case), the PR_RTF_COMPRESSED option of (extended) MAPI may help you here. Also look at this.

Tobiasopdenbrouw
+1  A: 

Indy supports sending RTF emails. Send an email the same way you would send an HTML email, just use the "text/rtf", "text/enriched", or "text/richtext" Context-Type, and send the raw RTF codes that are generated by TRichEdit when its PlainText property is set to false. Also, if you are using Indy 10, look at its TIdMessageBuilderRtf class to set up the TIdMessage structure correctly.

Remy Lebeau - TeamB
is there an example that shows how to do? I tried with this but I get a wrong message:procedure SendMailRtf;var MBuilder: TIdMessageBuilderRtf;begin MBuilder := TIdMessageBuilderRtf.Create; MBuilder.RtfType := idMsgBldrRtfRichtext; MBuilder.Rtf := RichEdit1.Lines; // gets rtf from reichedit MBuilder.FillMessage(IdMessage1); MBuilder.Free; IdSMTP1.Connect; IdSMTP1.Send(IdMessage1); IdSMTP1.Disconnect;end;
Tried to find an example for you. Didn't get one, but did get one workaround given to someone that was looking for the same solution. Have a look at http://www.nldelphi.com/forum/showthread.php?t=15822 . (In dutch: google translate may be your friend). They also mention jvRichEditToHTML.
Tobiasopdenbrouw
The TRichEdit.Lines property does not provide access to the RTF codes that are needed for an RTF email. Set the TRichEdit.PlainText property to False and call the TRichEdit.SaveToStream() method instead, and then pass the resulting TStream to the TIdMessageBuilderRtf.RTF.LoadFromStream() method.
Remy Lebeau - TeamB
@Remy - I tried as you suggested with the TStream Idea but I didn't succeed. Could you please tell me what is wrong in my code? (I updated my question adding the code). By the way I used TRichEdit.Lines.SaveToStream not TRichEdit.SaveToStream (that doesn't exist).
@user193655: you are not setting the stream's Position property back to 0 after calling SaveToStream(), so there is nothing for LoadFromStream() to load.
Remy Lebeau - TeamB