【问题标题】:Mimekit adds the rtf as an attachment and not the bodyMimekit 添加 rtf 作为附件而不是正文
【发布时间】:2017-02-09 14:45:45
【问题描述】:

使用以下代码将 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,因为它以以下内容开头:

内容类型:文本/rtf;名称=body.rtf

(换行)

【问题讨论】:

    标签: c# mailkit mimekit


    【解决方案1】:

    您获得Content-Type 标头的原因是因为您正在编写 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}");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 2013-06-24
      • 1970-01-01
      • 2012-01-24
      • 2016-06-04
      相关资源
      最近更新 更多