1

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

1 回答 1

0

我遇到过同样的问题。我在想这是防火墙或节流的问题。我终于找到了一个奇怪的解决方法。当 Exchange Web 服务 (EWS)FindItems不断抛出异常时,我打开浏览器并登录到网络上的帐户(即,打开outlook.office365.com)。这可以解决大约 1 小时或更长时间的问题。

这不仅是 EWS 问题,也是 Outlook 无法连接到 Exchange 服务器时的问题。OWA 登录也解决了这个问题。

于 2020-05-20T05:24:40.813 回答