Indy 9 - 使用身体作为RTF和附件发送电子邮件

Bet*_*eto 3 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
   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...
Run Code Online (Sandbox Code Playgroud)

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

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

  • 文/ RTF
  • 文/富

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

Rem*_*eau 5

TIdMessage.ContentTypeTIdAttachment存在时,您将设置为错误的值.它需要设置'multipart/mixed',而不是因为你是混合'multipart/alternative''application/x-zip-compressed'部分一起在同一个顶层MIME嵌套级别,而'text/...'部分是孩子们的'multipart/alternative'一部分,而不是.

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

HTML消息

您尝试创建的电子邮件结构包含在"纯文本和HTML和附件:仅与非相关的附件"部分中.您只需用RTF替换HTML,并忽略该部分的TIdText对象,'multipart/alternative'因为TIdMessageIndy 9将在内部为您创建(在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...
Run Code Online (Sandbox Code Playgroud)