2

我在使用ews managed api導出office 365帳戶中的電子郵件時看到錯誤,「服務器無法立即爲此請求提供服務,請稍後再試。」爲什麼會發生這種錯誤,並且可以做些什麼呢?EWS服務器目前無法處理此請求

我使用下面的代碼的工作: -

  _GetEmail = (EmailMessage)item; 
      bool isread = _GetEmail.IsRead; 
      sub = _GetEmail.Subject; 
      fold = folder.DisplayName; 
      historicalDate = _GetEmail.DateTimeSent.Subtract(folder.Service.TimeZone.GetUtcOffset(_GetEmail.DateTimeSent)); 
           props = new PropertySet(EmailMessageSchema.MimeContent); 
           var email = EmailMessage.Bind(_source, item.Id, props); 

           bytes = new byte[email.MimeContent.Content.Length]; 
           fs = new MemoryStream(bytes, 0, email.MimeContent.Content.Length, true); 
           fs.Write(email.MimeContent.Content, 0, email.MimeContent.Content.Length); 

           Demail = new EmailMessage(_destination); 
           Demail.MimeContent = new MimeContent("UTF-8", bytes); 

           // 'SetExtendedProperty' used to maintain historical date of items 
           Demail.SetExtendedProperty(new ExtendedPropertyDefinition(57, MapiPropertyType.SystemTime), historicalDate); 
           // PR_MESSAGE_DELIVERY_TIME 
           Demail.SetExtendedProperty(new ExtendedPropertyDefinition(3590, MapiPropertyType.SystemTime), historicalDate); 
           if (isread == false) 
           { 
            Demail.IsRead = isread; 
           } 

           if (_source.RequestedServerVersion == flagVersion && _destination.RequestedServerVersion == flagVersion) 
           { 
            Demail.Flag = _GetEmail.Flag; 
           } 

           _lstdestmail.Add(Demail); 

           _objtask = new TaskStatu(); 
           _objtask.TaskId = _taskid; 
           _objtask.SubTaskId = subtaskid; 
           _objtask.FolderId = Convert.ToInt64(folderId); 
           _objtask.SourceItemId = Convert.ToString(_GetEmail.InternetMessageId.ToString()); 
           _objtask.DestinationEmail = Convert.ToString(_fromEmail); 
           _objtask.CreatedOn = DateTime.UtcNow; 
           _objtask.IsSubFolder = false; 
           _objtask.FolderName = fold; 
           _objdbcontext.TaskStatus.Add(_objtask); 
           try 
           { 
            if (counter == countGroup) 
            { 
             Demails = new EmailMessage(_destination); 
             Demails.Service.CreateItems(_lstdestmail, _destinationFolder.Id, MessageDisposition.SaveOnly, SendInvitationsMode.SendToNone); 
             _objdbcontext.SaveChanges(); 
             counter = 0; 
             _lstdestmail.Clear(); 
            } 
           } 

           catch (Exception ex) 
           { 
            ClouldErrorLog.CreateError(_taskid, subtaskid, ex.Message + GetLineNumber(ex, _taskid, subtaskid), CreateInnerException(sub, fold, historicalDate)); 
            counter = 0; 
            _lstdestmail.Clear(); 
            continue; 
           } 

此錯誤僅發生試圖在辦理出口365個賬戶,在2010年的前景,2013年,2016年等的情況下正常工作..

+0

被_destination定義,其中。 – Seabizkit

+0

@Seabizkit我在全球範圍內定義的目的地不在功能級別 –

回答

0

通常情況下,在Exchange中超出EWS限制時就是這種情況。這是在here解釋。

確保您已經知道限制策略並且您的代碼符合它們。 如果您有服務器,則可以使用Get-ThrottlingPolicy找到限制策略。

+0

您能否介紹一下如何在c#中使用ews定義限制策略? –

0

解決您遇到的限制問題的一種方法是實現分頁,而不是一次請求所有項目。你可以參考this link

例如:

using Microsoft.Exchange.WebServices.Data; 

static void PageSearchItems(ExchangeService service, WellKnownFolderName folder) 
{ 
    int pageSize = 5; 
    int offset = 0; 

    // Request one more item than your actual pageSize. 
    // This will be used to detect a change to the result 
    // set while paging. 
    ItemView view = new ItemView(pageSize + 1, offset); 

    view.PropertySet = new PropertySet(ItemSchema.Subject); 
    view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending); 
    view.Traversal = ItemTraversal.Shallow; 

    bool moreItems = true; 
    ItemId anchorId = null; 
    while (moreItems) 
    { 
     try 
     { 
      FindItemsResults<Item> results = service.FindItems(folder, view); 
      moreItems = results.MoreAvailable; 

      if (moreItems && anchorId != null) 
      { 
       // Check the first result to make sure it matches 
       // the last result (anchor) from the previous page. 
       // If it doesn't, that means that something was added 
       // or deleted since you started the search. 
       if (results.Items.First<Item>().Id != anchorId) 
       { 
        Console.WriteLine("The collection has changed while paging. Some results may be missed."); 
       } 
      } 

      if (moreItems) 
       view.Offset += pageSize; 

      anchorId = results.Items.Last<Item>().Id; 

      // Because you’re including an additional item on the end of your results 
      // as an anchor, you don't want to display it. 
      // Set the number to loop as the smaller value between 
      // the number of items in the collection and the page size. 
      int displayCount = results.Items.Count > pageSize ? pageSize : results.Items.Count; 

      for (int i = 0; i < displayCount; i++) 
      { 
       Item item = results.Items[i]; 

       Console.WriteLine("Subject: {0}", item.Subject); 
       Console.WriteLine("Id: {0}\n", item.Id.ToString()); 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Exception while paging results: {0}", ex.Message); 
     } 
    } 
} 
相關問題