EWS服务器现在无法为此请求提供服务

时间:2016-02-26 08:23:31

标签: exchange-server office365 exchangewebservices ews-managed-api

我在使用ews托管api在Office 365帐户中导出电子邮件时看到错误,"服务器现在无法为此请求提供服务。稍后再试。"为什么会发生错误以及可以采取哪些措施呢?

我正在使用以下代码进行该项工作: -

          _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;
                                }

仅当尝试在Office 365帐户中导出时才会出现此错误,并且在Outlook 2010,2013,2016等情况下正常工作。

2 个答案:

答案 0 :(得分:0)

通常在超过Exchange中的EWS限制时就是这种情况。它在here中解释。

确保您已了解限制政策,并且您的代码符合这些政策。 如果您有服务器,则可以使用Get-ThrottlingPolicy找到限制策略。

答案 1 :(得分: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);
        }
    }
}