2017-02-09 76 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}"); 
    } 
} 
相關問題