Mimekit 添加 rtf 作为附件而不是正文

Mimekit adds the rtf as an attachment and not the body

使用以下代码,winmail.dat 文件的 rtf 正文作为附件添加到已保存的电子邮件中,而不是正文:

using (Stream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
{
    MimeKit.MimeMessage mimeMessage = MimeKit.MimeMessage.Load(stream);

    int i = 1;
    foreach (MimeKit.MimePart attachment in mimeMessage.Attachments)
    {
        if (attachment.GetType() == typeof(MimeKit.Tnef.TnefPart))
        {
            MimeKit.Tnef.TnefPart tnefPart = (MimeKit.Tnef.TnefPart)attachment;

            MimeKit.MimeMessage tnefMessage = tnefPart.ConvertToMessage();
            tnefMessage.WriteTo(path + $"_tnefPart{i++}.eml");
        }
    }
}

我该如何解决这个问题?


查看 Attachments 它不存在,但附件和 body.rtf 文件存在于 BodyParts 中。所以我可以这样得到 body.rtf 文件:

int b = 1;
foreach (MimeKit.MimeEntity bodyPart in tnefMessage.BodyParts)
{
    if (!bodyPart.IsAttachment)
    {
        bodyPart.WriteTo(path + $"_bodyPart{b++}.{bodyPart.ContentType.MediaSubtype}");
    }
}

旁注:body.rtf 文件是否不是真正的 rtf,因为它以以下内容开头:

Content-Type: text/rtf; name=body.rtf

(new line)

您得到 Content-Type header 的原因是因为您正在编写 MIME 信封和内容。

你需要做的是:

int b = 1;
foreach (MimeKit.MimeEntity bodyPart in tnefMessage.BodyParts)
{
    if (!bodyPart.IsAttachment)
    {
        var mime = (MimeKit.MimePart) bodyPart;
        mime.ContentObject.DecodeTo(path + $"_bodyPart{b++}.{bodyPart.ContentType.MediaSubtype}");
    }
}