2015-07-20 115 views
0

我已經編寫了一種將Outlook電子郵件附件保存到硬盤並將文件轉換爲Base64String的方法。當我保存附件時,嵌入的(內嵌)圖像也會保存,即使它們不是真正的附件。我只想保存真實的附件。然後我修改瞭如下方法。但是現在我得到了「.OfType()」行的錯誤。如何在保存Outlook電子郵件附件時避免嵌入(內嵌)圖像

這裏是我的代碼:

private string GetBase64StringForAttachments(Outlook.MailItem mailItem) 
     { 
      StringBuilder builder = new StringBuilder(); 
      Outlook.Attachments mailAttachments = mailItem.Attachments; 

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

        for (int i = 1; i <= mailAttachments.Count; i++) 
        { 
         Outlook.Attachment currentAttachment = mailAttachments[i]; 

         bool isMatch = matches 
          .OfType<Match>() 
          .Select(m => m.Value) 
          .Where(s => s.IndexOf("cid:" + currentAttachment.FileName, StringComparison.InvariantCultureIgnoreCase) >= 0) 
          .Any(); 

         MessageBox.Show(currentAttachment.FileName + ": " + (isMatch ? "Inline Image" : "Attached Image")); 

         if (currentAttachment != null) 
         { 

          string date = DateTime.Now.ToString("yyyymmddhhmmss"); 
          string path = "C:\\test\\" + date + currentAttachment.FileName; //ToDo: Create Folder 
          currentAttachment.SaveAsFile(path); 

          FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); 
          byte[] filebytes = new byte[fs.Length]; 
          fs.Read(filebytes, 0, Convert.ToInt32(fs.Length)); 
          string encodedData = Convert.ToBase64String(filebytes, Base64FormattingOptions.InsertLineBreaks); 
          builder.Append(encodedData).Append(","); 

          Marshal.ReleaseComObject(currentAttachment); 
         } 
        } 

        if (builder.Length > 0) 
        {       
         string encodedAttachments = builder.ToString().Remove(builder.ToString().Length-1); 
         return builder.ToString(); 
        } 
        else 
         return ""; 

       } 
       else return ""; 
      } 
      catch (Exception ex) 
      { 
       Debug.DebugMessage(2, "Error in GetBase64StringForAttachments : in AddinModule " + ex.Message); 
       return ""; 
      } 
      finally 
      { 
       Marshal.ReleaseComObject(mailAttachments);     
      } 
     } 

這是錯誤消息:

'System.Text.RegularExpressions.MatchCollection' 不包含 'OfType' 和一個沒有定義可以找到接受'System.Text.RegularExpressions.MatchCollection'類型的第一個參數的擴展方法'OfType'(你是否缺少使用指令或程序集引用?)

我需要什麼:

  1. 我不是LINQ的大風扇。所以,你能請這個建議我

  2. 有沒有更好的方式來做到這一點?

    我已經嘗試過遵循這些問題的參考答案,他們沒有爲我

Saving only the REAL attachments of an Outlook MailItem

Don't save embed image that contain into attachements (like signature image)

工作
  • 是否有使用Redemption來區分真實附件的方法?
  • 回答

    1

    是的,Redemption暴露了RDOAttachmentHidden屬性 - 它檢查HTML主體以確保附件不被用作嵌入圖像。

    另請注意,您可以使用RDOAtttachment訪問附件數據。而不是先將附件另存爲文件。

    Redemption.RDOSession rSession = new Redemption.RDOSession(); 
    rSession.MAPIOBJECT = mailItem.Application.Session.MAPIOBJECT; 
    Redemption.RDOMail rMail= rSession.GetRDOFolderFromOutlookObject(mailItem) 
    foreach (Redemption.RDOAttachment attach in rMail.Attachments) 
    { 
        if ((attach.Type == Redemption.rdoAttachmentType.olByValue) && (!attach.Hidden)) 
        { 
        attach.SaveAsFile(path); 
        } 
    } 
    next 
    
    相關問題