2012-05-04 25 views
5

嗨我必須從使用C#的Outlook 2010在本地目錄中單獨閱讀附件和內聯圖片。我爲此使用了屬性和內容ID概念。我正在使用下面的代碼來做這件事,但它現在正在工作。如何在Outlook 2010中區分內聯圖片和附件[C#]

if (mailItem.Attachments.Count > 0) 
{ 
    /*for (int i = 1; i <= mailItem.Attachments.Count; i++) 
    { 
    string filePath = Path.Combine(destinationDirectory, mailItem.Attachments[i].FileName); 
    mailItem.Attachments[i].SaveAsFile(filePath); 
    AttachmentDetails.Add(filePath); 
    }*/ 

    foreach (Outlook.Attachment atmt in mailItem.Attachments) 
    { 
     MessageBox.Show("inside for each loop"); 
     prop = atmt.PropertyAccessor; 
     string contentID = (string)prop.GetProperty(SchemaPR_ATTACH_CONTENT_ID); 
     MessageBox.Show("content if is " +contentID); 

     if (contentID != "") 
     { 
      MessageBox.Show("inside if loop"); 
      string filePath = Path.Combine(destinationDirectory, atmt.FileName); 
      MessageBox.Show(filePath); 
      atmt.SaveAsFile(filePath); 
      AttachmentDetails.Add(filePath); 
     } 
     else 
     { 
      MessageBox.Show("inside else loop"); 
      string filePath = Path.Combine(destinationDirectoryT, atmt.FileName); 
      atmt.SaveAsFile(filePath); 
      AttachmentDetails.Add(filePath); 
     } 
    } 
} 

請幫助工作正在進行中....

+0

串SchemaPR_ATTACH_CONTENT_ID = @ 「http://schemas.microsoft.com/mapi/proptag/0x3712001E」; – zytham

回答

2

我來到這裏尋找解決方案,但不喜歡在整個HTMLBody中搜索「cid:」的想法。首先,對每個文件名都做得很慢,其次,如果正文中出現「cid:」,我會得到誤報。另外,在HTMLBody上執行ToLower()並不是一個好主意。

取而代之,我最終在HTMLBody上使用了一個正則表達式來查找任何<img>標籤的實例。因此,無法在正文中錯誤匹配「cid:」(但不太可能)。

 Regex reg = new Regex(@"<img .+?>", RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); 
    MatchCollection matches = reg.Matches(mailItem.HTMLBody); 

    foreach (string fileName in attachments.Select(a => a.FileName) 
    { 
     bool isMatch = matches 
      .OfType<Match>() 
      .Select(m => m.Value) 
      .Where(s => s.IndexOf("cid:" + fileName, StringComparison.InvariantCultureIgnoreCase) >= 0) 
      .Any(); 

     Console.WriteLine(fileName + ": " + (isMatch ? "Inline" : "Attached")); 
    } 

我很確定我可以寫一個正則表達式來返回文件名,它可能會更有效。但是我寧願爲了可讀性而爲額外的開支付費,因爲那些不是正則表達式的大師們必須維護代碼。

+0

這不會工作,如果郵件有一個簽名與其中的某種圖片。 展望將不包括在標記下。 – Steinfeld

+0

是的,當從css屬性引用嵌入圖像時,這不起作用。另外,我注意到訪問HtmlBody和RtfBody將郵件設置爲髒(請求保存關閉)作爲副作用:/ – Yaurthek

相關問題