在delphi中发送带有rtf文本的电子邮件
我想执行以下任务:将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
显然(我自己用谷歌搜索了这个,因为我从 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.
您可以使用 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.php3:您可以将
noname
附件重命名为NONAME.MHT
,然后就可以用 Internet Explorer 打开它了。但是您可能需要考虑使用 HTML 编辑控件,而不是 TRichEdit。那么你就不用担心转换了。像这样的问题:-
Delphi 的 WYSIWYG HTML 编辑器组件
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.php3: You can rename the
noname
attachment asNONAME.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