【问题标题】:Indy 9 - Email with body as RTF and with attachments [duplicate]Indy 9 - 正文为 RTF 并带有附件的电子邮件 [重复]
【发布时间】:2013-03-13 19:55:23
【问题描述】:

我正在尝试使用 indy 9 发送电子邮件:

  • 正文为 RTF,从 TRichEdit 格式化
  • 附加一个文件

代码:

 Message := TIdMessage.Create()
 Message.Recipients.EMailAddresses := 'someone@domain.dotcom';

 Message.ContentType := 'multipart/alternative';

 with TIdText.Create(Message.MessageParts) do
   ContentType := 'text/plain';

 with TIdText.Create(Message.MessageParts) do
 begin
   ContentType := 'text/richtext';
   Body.LoadFromFile('c:\bodymsg.rtf');
 end;

 TIdAttachment.Create(Message.MessageParts, 'c:\myattachment.zip');

 // send...

结果:正文为空(使用 web gmail 和 Outlook 2010 作为客户端)。

我已经尝试过其他内容类型但没有成功:

  • 文本/rtf
  • 文本/丰富

注意:我不会升级到 Indy 10。

【问题讨论】:

  • indy10 出局的合理原因是什么? Indy9 真的很老了,有一些 smtp 相关的 bug。
  • Remy Lebeau 对链接副本(包括他的 cmets)的回答似乎回答了这个问题。 (不过,您必须阅读 cmets,这可能是他的答案在那里不被接受的原因。)关键是使用 TRichEdit.SaveToStreamBody.LoadFromStream 而不是 Body.LoadFromFile,因为 LoadFromFile 假定为纯文本。
  • @KenWhite:还请记住,链接的副本是针对 Indy 10 的。由于 Indy 9 缺乏深度 MIME 支持,Indy 9 的这种情况需要一些不同的方法。至于您对LoadFromStream()LoadFromFile() 的评论,LoadFromFile() 在内部调用LoadFromStream(),而TIdText.Body 只是TStrings,因此它将按原样加载文件的RTF 编码。
  • @Remy:在骗局中没有抓住 Indy 10。无论如何,我知道你会回答这个问题。 ;-)
  • @whosrdaddy:是的,代码适应,高影响!

标签: delphi indy


【解决方案1】:

TIdAttachment 存在时,您将TIdMessage.ContentType 设置为错误的值。它需要设置为'multipart/mixed',因为您将'multipart/alternative''application/x-zip-compressed' 部分混合在同一顶级MIME 嵌套级别,而'text/...' 部分是'multipart/alternative' 部分的子代。

看看我在 Indy 网站上写的以下博客文章:

HTML Messages

“纯文本和 HTML 及附件:仅不相关的附件”部分涵盖了您尝试创建的电子邮件结构。您只需将 HTML 替换为 RTF,并忽略 'multipart/alternative' 部分的 TIdText 对象,因为 Indy 9 中的 TIdMessage 将为您在内部创建它(在 Indy 10 中明确需要它,因为它比 Indy 更深入地支持 MIME 9 个)。

试试这个:

Message := TIdMessage.Create()
Message.Recipients.EMailAddresses := 'someone@domain.dotcom';

Message.ContentType := 'multipart/mixed';

with TIdText.Create(Message.MessageParts) do
begin
  ContentType := 'text/plain';
  Body.Text := 'You need an RTF reader to view this message';
end;

with TIdText.Create(Message.MessageParts) do
begin
  ContentType := 'text/richtext';
  Body.LoadFromFile('c:\bodymsg.rtf');
end;

TIdAttachment.Create(Message.MessageParts, 'c:\myattachment.zip');

// send...

【讨论】:

  • 上面的代码不起作用!对不起...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-24
  • 2012-11-28
  • 1970-01-01
  • 2012-06-12
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多