2017-02-09 185 views
0

使用下面的代码Winmail.dat文件的RTF主体作为附件添加到保存的电子邮件没有身体:Mimekit添加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,因为它具有以下开始:

内容-Type:text/rtf;名称= body.rtf

(新线)

回答

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}"); 
    } 
}