在delphi中发送带有rtf文本的电子邮件

发布于 2024-09-11 10:31:46 字数 1104 浏览 9 评论 0原文

我想执行以下任务:将 TRichEdit 内容(rtf 文本)转换为非纯文本电子邮件正文。

MAPI 不支持 rtf,但是有没有办法用 Indy 来实现呢?

问题是 rtf 是 rtf,而电子邮件是纯文本或 HTML。

有人可以建议一个技巧吗?是否可以使用 TWebBrowser 将 rtf 转换为文本?

基本上情况是:
1)用户以delphi形式编写电子邮件,
2)然后电子邮件使用MAPI发送到默认邮件客户端(因此生成一个新的电子邮件窗口,并且消息正文与我在delphi形式中的相同)
3) 用户从邮件客户端发送电子邮件

无论如何,MAPI 只接受纯文本。

更新:

尝试使用 Indy 我写了这个,但它仍然不起作用,当我将邮件发送到我的 gmail 帐户时,我收到一条带有空正文和 NONAME 假附件的消息。

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云淡风轻 2024-09-18 10:31:46

Indy 支持发送 RTF 电子邮件。发送电子邮件的方式与发送 HTML 电子邮件相同,只需使用“text/rtf”、“text/enriched”或“text/richtext”上下文类型,然后发送由 TRichEdit 生成的原始 RTF 代码其 PlainText 属性设置为 false。另外,如果您使用的是 Indy 10,请查看其 TIdMessageBuilderRtf 类以正确设置 TIdMessage 结构。

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.

好倦 2024-09-18 10:31:46

显然(我自己用谷歌搜索了这个,因为我从 Delphi 邮寄了一些,但在本例中不是),(扩展)MAPI 的 PR_RTF_COMPRESSED 选项可能会在这里帮助你。另请参阅

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.

熟人话多 2024-09-18 10:31:46

是否可以使用 TWebBrowser 将 rtf 转换为文本?

您可以使用 TRichEdit 将 RTF 转换为文本 - 但显然这会丢失所有格式:

在运行时使用 TRichEdit,无需定义父级

2:您可以在线找到许多 RTF 到 HTML 转换器 - 有些是免费的,有些不是。像这样:

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:您可以将 noname 附件重命名为 NONAME.MHT,然后就可以用 Internet Explorer 打开它了。

但是您可能需要考虑使用 HTML 编辑控件,而不是 TRichEdit。那么你就不用担心转换了。像这样的问题:-

Delphi 的 WYSIWYG HTML 编辑器组件

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文