我正在使用EWS从收件箱中获取附件。但是我不时得到错误。
[SocketException(0x2746):现有连接被远程主机强行关闭]
我假设这是事物交换方面的某种限制。所以我的问题是。如何更改代码以正确处理这种错误。
string UserName = Properties.Settings.Default.UserName;
string Password = Properties.Settings.Default.Password;
string InboxEmail = Properties.Settings.Default.InboxEmail;
string SavePath = Properties.Settings.Default.SavePath;
int ItemViewCount = Properties.Settings.Default.ItemViewCount;
bool moreItems = true;
ItemId anchorId = null;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new NetworkCredential(UserName, Password);
service.Url = new Uri("https://myexchangeserver/EWS/Exchange.asmx");
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
FolderId SharedMailbox = new FolderId(WellKnownFolderName.Inbox, InboxEmail);
ItemView itemView = new ItemView(ItemViewCount + 1
, 0);
itemView.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
FindItemsResults<Item> findResults;
// we need to loop through the pages
while (moreItems)
{
findResults = service.FindItems(SharedMailbox, searchFilter, itemView);
anchorId = findResults.Items.First<Item>().Id;
// do stuff here
// see if more is available over the limit of 1k
moreItems = findResults.MoreAvailable;
if (moreItems)
{
itemView.Offset += ItemViewCount;
}
// Set the flag to discontinue paging.
if (!findResults.MoreAvailable)
{
moreItems = false;
}
}
答案 0 :(得分:0)
我有同样的问题。我当时以为这是防火墙或限制的问题。我终于找到了一个怪异的解决方法。当Exchange Web服务(EWS)的FindItems
不断抛出异常时,我打开浏览器并登录到Web帐户(即outlook.office365.com
)。这样可以解决问题大约1小时或更长时间。
这不仅是EWS问题,还是Outlook无法连接到Exchange服务器时的问题。 OWA登录也可以解决该问题。