从 EWS(exchange web 服务)服务器读取的电子邮件无法处理此请求错误 c#

email read from EWS (exchange web service) server cannot process this request error c#

我有一个任务,我需要检查发送到我邮箱的电子邮件并阅读它们,根据主题我必须做一些任务。但出于演示目的,我只提供了更新电子邮件阅读状态的基本功能

基本连接和创建服务对象一切正常:

///////////

    NetworkCredential credentials = new NetworkCredential(securelyStoredEmail, securelyStoredPassword);

    ExchangeService _service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
_service.Credentials = credentials;

    _service.AutodiscoverUrl("User1@contoso.com");

//////////////////////// 这里一切正常。但是,我将使用反应式 linq 的可观察事件每 60 秒调用一次以下方法。这是去轮询我的邮箱,每 60 秒阅读 100 封电子邮件。 一切正常,直到某个时候。有时,当控件到达 parallel.foreach 循环内的代码行时,它会显示类似 'server cannot process this request now. Please try later' 之类的错误消息。这个错误恰好出现在

var email = EmailMessage.Bind(_service, findItemsResult.Id, emailProps);

所以每 60 秒,我就会收到此错误 sometimes.sometimes 它工作正常。 下面是每 60 秒执行一次的方法。就像我尝试每 60 秒阅读一次来自 "myaccount.com" 的电子邮件,但我收到错误 'server cannot process'.

internal void GetEmailsFrommymailbox()
{ 

try
{

var view = new ItemView(100);
var userMailbox = new Mailbox(userMailbox);
var folderId = new FolderId(WellKnownFolderName.Inbox, userMailbox);
SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));



var findResults = _service.FindItems(folderId, sf, view);
var emailProps = new PropertySet(ItemSchema.MimeContent, ItemSchema.Body,
ItemSchema.InternetMessageHeaders);

Parallel.ForEach(findResults, findItemsResult =>
{


///////////// this is the line where i get error////////

    var email = EmailMessage.Bind(_service, findItemsResult.Id, emailProps);

//// above is the place where i get error

var emailMatching = email;
try
{
email.IsRead = true;
email.Update(ConflictResolutionMode.AutoResolve);

}
catch (Exception emailreadFromBccException)
{
Logger.Warn(emailreadFromBccException + " Unable to update email read status");
}


});
}

}

你收到这个错误是因为你被限制了 https://msdn.microsoft.com/en-us/library/office/jj945066%28v=exchg.150%29.aspx 并且你被限制是因为你的代码效率不高。

而不是做

Parallel.ForEach(findResults, findItemsResult => {

///////////// 这是我出错的行////////

var email = EmailMessage.Bind(_service, findItemsResult.Id, emailProps);

您应该使用 LoadPropertiesFromItems http://blogs.msdn.com/b/exchangedev/archive/2010/03/16/loading-properties-for-multiple-items-with-one-call-to-exchange-web-services.aspx。这将减少您需要对服务器进行的调用次数。

我还建议您使用流式通知 https://msdn.microsoft.com/en-us/library/office/hh312849%28v=exchg.140%29.aspx?f=255&MSPPError=-2147217396,这意味着您不需要每 60 秒轮询一次服务器,只需在新项目到达时采取行动。

干杯 格伦