Send an e-mail with rtf text in delphi
Asked Answered
M

3

5

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;
Meliorate answered 22/7, 2010 at 13:17 Comment(0)
W
7

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.

Worsley answered 22/7, 2010 at 20:59 Comment(5)
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;Meliorate
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 nldelphi.com/forum/showthread.php?t=15822 . (In dutch: google translate may be your friend). They also mention jvRichEditToHTML.Mandel
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.Worsley
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).Meliorate
@user193655: you are not setting the stream's Position property back to 0 after calling SaveToStream(), so there is nothing for LoadFromStream() to load.Worsley
M
1

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.

Mandel answered 22/7, 2010 at 14:10 Comment(1)
I'd like to stop using MAPI, I'd prefer to sent emails through idSMTP directlyMeliorate
D
1

Is it possible to convert rtf to text using TWebBrowser?

You can convert RTF to Text using TRichEdit - but obviously this loses all formatting:

Using TRichEdit at runtime without defining a parent

2: You can find a number of RTF to HTML converters online - some free, some not. Like these:

http://wiki.delphi-jedi.org/wiki/JVCL_Help:TJvRichEditToHtml https://www.habarisoft.com/scroogexhtml_delphi.html http://www.easybyte.com/products/rtf2html.html

http://www.sautinsoft.com/products/rtf-to-html/index.php

3: You can rename the noname attachment as NONAME.MHT and it should then be openable with internet explorer.

However you might want to consider using an HTML edit control, rather than a TRichEdit. Then you have no conversion to worry about. Something like this question:-

WYSIWYG HTML Editor Component for Delphi

Disjoint answered 19/2, 2015 at 17:28 Comment(2)
@user193655 - Ah, yeah :) I think your problem is that gmail (and many other clients) don't support RTF in emails, so conversion to HTML is the only way. Have you looked at any of those RTF to HTML converters?Disjoint
Good to know about gmail and rtf, so rtf is a way to discard. The converters you linekd are .net ones, I'd rather prefer a VCL component or a win32 library. Probably rtf to html is my only solution.Meliorate

© 2022 - 2024 — McMap. All rights reserved.