2012-09-04 41 views
8

我需要從郵件中獲取並保存附件,但是使用下面的代碼會返回所有附件 - 這意味着它還會返回嵌入的圖像,如發件人的簽名以及圖像的徽標。我如何區分真正的附件與嵌入式圖像?我從論壇上看到很多,但對我而言仍然不清楚。vsto +區分附件

public static void SaveData(MailItem currentMailItem) 
{ 
    if (currentMailItem != null) 
    {  
     if (currentMailItem.Attachments.Count > 0) 
     { 
      for (int i = 1; i <= currentMailItem.Attachments.Count; i++) 
      { 
       currentMailItem.Attachments[i].SaveAsFile(@"C:\TestFileSave\" + currentMailItem.Attachments[i].FileName); 
      } 
     } 
    } 
} 

回答

8

您可以檢查附件是否在線或者不使用以下pseudo-code from MS Technet Forums

if body format is plain text then 
    no attachment is inline 
else if body format is RTF then 
    if PR_ATTACH_METHOD value is 6 (ATTACH_OLE) then 
    attachment is inline 
    else 
    attachment is normal 
else if body format is HTML then 
    if PR_ATTACH_FLAGS value has the 4 bit set (ATT_MHTML_REF) then 
    attachment is inline 
    else 
    attachment is normal 

可以使用Attachment.PropertyAccessor訪問使用MailItem.BodyFormat消息體格式MIME附件屬性

string PR_ATTACH_METHOD = 'http://schemas.microsoft.com/mapi/proptag/0x37050003'; 
var attachMethod = attachment.PropertyAccessor.Get(PR_ATTACH_METHOD); 

string PR_ATTACH_FLAGS = 'http://schemas.microsoft.com/mapi/proptag/0x37140003'; 
var attachFlags = attachment.PropertyAccessor.Get(PR_ATTACH_FLAGS); 
+1

非常感謝你!有用! – Liz