Delphi Indy 9-邮件正文为RTF并带有附件

Delphi Indy 9-邮件正文为RTF并带有附件,delphi,indy,Delphi,Indy,我正在尝试用indy 9发送电子邮件: 主体为RTF,由TRichEdit格式化 附上一份文件 代码: Message := TIdMessage.Create() Message.Recipients.EMailAddresses := 'someone@domain.dotcom'; Message.ContentType := 'multipart/alternative'; with TIdText.Create(Message.MessageParts) do C

我正在尝试用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。

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

请看我在印地大学网站上写的以下博客文章:

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

试试这个:

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...

为什么indy10被排除在外,有什么合理的理由吗??Indy9确实很老了,有一些与smtp相关的bug。Remy Lebeau对链接副本的回答(包括他的评论)似乎回答了这个问题。(不过,你必须阅读评论,这可能就是为什么他的答案没有被接受的原因。)关键是使用
TRichEdit.SaveToStream
Body.LoadFromStream
,而不是
Body.LoadFromFile
,正如
LoadFromFile
假定为纯文本。@KenWhite:还要记住链接的副本是针对Indy 10的。Indy 9的这种情况需要一点不同的方法,因为Indy 9缺乏深度MIME支持。至于您对
LoadFromStream()
vs
LoadFromFile()
的评论,
LoadFromFile()
在内部调用
LoadFromStream()
,而
TIdText.Body
只是一个
TStrings
,因此它将按原样加载文件的RTF编码。@Remy:在复制中没有捕获Indy 10。无论如何,我知道你会来回答这个问题的@谁爸爸:是的,代码改编,影响很大!上面的代码不起作用!很抱歉