2016-10-18 107 views
0

我們有以下代碼來使用EWS讀取附件。使用Microsoft Exchange Web服務從郵件中讀取附件

 FindItemsResults<Item> foundItems = service.FindItems(FolderId, new ItemView(1000)); 

    var orderItems = from list in foundItems 
        orderby list.DateTimeReceived 
        select list; 
    foreach (EmailMessage item in orderItems) 
    { 
     item.Load();//SARANYA 
     EmailMessage foundEmail = (EmailMessage)item; 
     EmailMessage message = EmailMessage.Bind(service, new ItemId(item.Id.ToString()), new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments, ItemSchema.Body)); 
     if (message.Attachments.Count > 0) 
     { 
      FileAttachment[] attachments = null; 
      attachments = new FileAttachment[message.Attachments.Count]; 

      foreach (Attachment attachment in message.Attachments) 
      { 
       try 
       { 
        if (attachment is FileAttachment) 
        { 

         FileAttachment fileAttachment = attachment as FileAttachment; 
         // System.Threading.Thread.Sleep(2000); 

         fileAttachment.Load(); 
         byte[] FolloupMailFileAttachmentContentBytes = fileAttachment.Content; 
         bool isScreenshot = false; 
         string ScreenfileName = ""; 

         for (int i = 0; i < imgSrcs.Count; i++) 
         { 
          if (imgSrcs[i].ToString() == fileAttachment.Name.ToString()) 
          { 
           isScreenshot = true; 
           if (!imgSrcs[i].ToString().Contains(".png")) 
            ScreenfileName = "cid:" + imgSrcs[i].ToString() + ".png"; 
           else 
            ScreenfileName = "cid:" + imgSrcs[i].ToString(); 
           break; 
          } 
         } 

         if (FolloupMailFileAttachmentContentBytes != null) 
         { 
          if (isScreenshot && RemoveSuccess == 1) 
          { 
           InsertMailItemAttachment(ScreenfileName, FolloupMailFileAttachmentContentBytes, caseid); 
          } 
          else 
           InsertMailItemAttachment(fileAttachment.Name.ToString(), FolloupMailFileAttachmentContentBytes, caseid); 
         } 
        } 
        else if (attachment is ItemAttachment) 
        { 
         item.Move(unreadmailFolder.Id); 
        } 
       } 
       catch (Exception exe) 
       { 
        if (!ReadMoved) 
        { 
         item.Move(readmailFolder.Id); 
         ReadMoved = true; 
        } 
        logfile.HandleError(exe, "Attachment Exception \n\nEmailbox - " + EMailBox + "\n\nEmail Subject - " + strSubject + " \n - Could not load the attachment (" + attachment.Name.ToString() + ")"); 

       } 
      } 
     } 
    } 

當我在fileattachment.load()之前提供thread.sleep()時,上面的代碼正在工作。當thread.sleep被刪除時,我得到下面的異常。

Error Source  : Microsoft.Exchange.WebServices 
Target Site   : Void InternalThrowIfNecessary() 
System Message  : The specified object was not found in the store. 
Stack Trace   :  at Microsoft.Exchange.WebServices.Data.ServiceResponse.InternalThrowIfNecessary() 
    at Microsoft.Exchange.WebServices.Data.ServiceResponse.ThrowIfNecessary() 
    at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute() 
    at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalGetAttachments(IEnumerable`1 attachments, Nullable`1 bodyType, IEnumerable`1 additionalProperties, ServiceErrorHandling errorHandling) 
    at Microsoft.Exchange.WebServices.Data.ExchangeService.GetAttachment(Attachment attachment, Nullable`1 bodyType, IEnumerable`1 additionalProperties) 
    at Microsoft.Exchange.WebServices.Data.Attachment.InternalLoad(Nullable`1 bodyType, IEnumerable`1 additionalProperties) 
    at Microsoft.Exchange.WebServices.Data.Attachment.Load() 
    at EMT_Office365_MailFetch_Scheduler.Program.FindEmail(Object threadState) in 

專家,請提供您的寶貴意見

+0

請格式化您的代碼,使其變得更可讀 –

+0

我已格式化相同。 – Saranya

回答

0

您的邏輯看起來不正確比如

item.Move(unreadmailFolder.Id); 

不應該是foreach循環中,如果你想在年底移動消息只需使用一個Flag並在完成附件處理後處理它(如果您有兩個ItemAttachment,則該邏輯不會運行)。睡眠正在發揮作用的原因很可能是Aysnc操作一旦完成移動就完成了。這就是爲什麼這應該是在foreach附加循環之外

+0

嗨格倫,你是對的。項目附件中的邏輯不正確,我會糾正它。該部分代碼從未執行,因爲我們的郵件不包含任何項目附件。我的查詢是關於文件附件部分,當我介紹睡眠時,附件會被讀取,但是當它被刪除時,會出現錯誤。我們的邏輯中沒有任何異步操作。我想知道這是否與許可等有關,但因爲它與睡眠有關,我也排除了這種選擇。 – Saranya

+0

但是我們有不同的線程處理不同的郵箱。這有什麼影響嗎? – Saranya

+0

如果你有多個線程在同一個項目上工作,那麼是的你有潛在的衝突更新。 –

相關問題