我正在使用 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;
}
}