mimekit outlook 将文本显示为附件

mimekit outlook show text as attachment

我有一个 word 文档并使用 Aspose.Word 执行邮件合并并将结果保存到内存流中作为 mhtml(我的代码的一部分):

Aspose.Words.Document doc = new Aspose.Words.Document(documentDirectory + countryLetterName);
doc.MailMerge.Execute(tempTable2);
MemoryStream outStream = new MemoryStream();
doc.Save(outStream, Aspose.Words.SaveFormat.Mhtml);

然后我使用 MimeKit(NuGet 的最新版本)发送消息:

outStream.Position = 0;
MimeMessage messageMimeKit = MimeMessage.Load(outStream);
messageMimeKit.From.Add(new MailboxAddress("<sender name>", "<sender email"));
messageMimeKit.To.Add(new MailboxAddress("<recipient name>", "<recipient email>"));
messageMimeKit.Subject = "my subject";
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
    client.Connect(<smtp server>, <smtp port>, true);
    client.Authenticate("xxxx", "pwd");
    client.Send(messageMimeKit);
    client.Disconnect(true);
}

在我的邮件 Web 客户端中打开收到的电子邮件时,我看到文本(带图像)和图像作为附件。

在Outlook(2016)中打开收到的邮件时,邮件正文是空的,我有两个附件,一个是文本,一个是图片。

查看 mht 内容本身,它看起来像:

MIME-Version: 1.0
Content-Type: multipart/related;
    type="text/html";
    boundary="=boundary.Aspose.Words=--"

This is a multi-part message in MIME format.

--=boundary.Aspose.Words=--
Content-Disposition: inline;
    filename="document.html"
Content-Type: text/html;
    charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Location: document.html

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; charset=
=3Dutf-8" /><meta http-equiv=3D"Content-Style-Type" content=3D"text/css" />=
<meta name=3D"generator" content=3D"Aspose.Words for .NET 14.1.0.0" /><titl=
e></title></head><body>
*****body removed *****
</body></html>

--=boundary.Aspose.Words=--
Content-Disposition: inline;
    filename="image.001.jpeg"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-Location: image.001.jpeg

****image content remove****

--=boundary.Aspose.Words=----

是否需要进行某种格式设置才能在 Outlook 中正确显示?还是因为找到了“3D”-关键词,比如content=3D"xxxx", style=3D"xxxx"?

提前致谢。

爱德华

=3D 位是 = 字符的 quoted-printable 编码。由于 headers 正确地将 Content-Transfer-Encoding 声明为 quoted-printable,所以这不是问题所在。

这里有一些关于尝试将内容整理成可在 Outlook 中使用的内容的建议(Outlook 非常挑剔):

MimeMessage messageMimeKit = MimeMessage.Load(outStream);
messageMimeKit.From.Add(new MailboxAddress("<sender name>", "<sender email"));
messageMimeKit.To.Add(new MailboxAddress("<recipient name>", "<recipient email>"));
messageMimeKit.Subject = "my subject";

var related = (MultipartRelated) messageMimeKit.Body;
var body = (MimePart) related[0];

// It's possible that the filename on the HTML body is confusing Outlook.
body.FileName = null;

// It's also possible that the Content-Location is confusing Outlook
body.ContentLocation = null;