EWS连接被远程主机强行关闭

问题描述 投票:1回答:1

我正在使用EWS从收件箱中获取附件。但是我不时得到错误。

[[SocketException(0x2746):现有连接被远程主机强行关闭]

enter image description here

我假设这是事物交换方面的某种限制。所以我的问题是。如何更改代码以正确处理此类错误。

            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;
                }
            }
c# exchangewebservices exchange-server-2016
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.