当前位置: 代码迷 >> Web Service >> 用ExchangeService取邮件异常The request failed. Unable to connect to the remote server
  详细解决方案

用ExchangeService取邮件异常The request failed. Unable to connect to the remote server

热度:274   发布时间:2016-05-02 02:18:37.0
用ExchangeService取邮件错误The request failed. Unable to connect to the remote server
我调用ExchangeService取邮件时, 在本地跑没有问题都能通过,但是放到测试服务机上跑的时候,有异常:The request failed. Unable to connect to the remote server
以下是我用的代码:

ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            
            service.Credentials = new NetworkCredential("testaccount", "passwd", "domain");
            service.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback);

            Microsoft.Exchange.WebServices.Data.SearchFilter.SearchFilterCollection sfcol = new SearchFilter.SearchFilterCollection();
            sfcol.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, title));
            
            sfcol.Add(new SearchFilter.IsGreaterThanOrEqualTo(MeetingMessageSchema.DateTimeReceived, twoMinutesBeforeNow));
            sfcol.Add(new SearchFilter.IsLessThanOrEqualTo(MeetingMessageSchema.DateTimeSent, now));

            try
            {
                FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, sfcol, new ItemView(1));

                service.LoadPropertiesForItems(findResults, PropertySet.FirstClassProperties);

                if (findResults.Items != null && findResults.Items.Count > 0)
                {                    
                    emailDetail = new Dictionary<string, string>();
                    emailDetail.Add(EmailTitle, title);
                    emailDetail.Add(EmailBody, findResults.Items.Last().Body.ToString());
                    emailDetail.Add(EmailToList, findResults.Items.Last().DisplayTo);

                }
            }
            catch (Exception ex)
            {
                Logger.Instance.WriteWarning(ex.Message);
                Logger.Instance.WriteWarning("Did not get the email!!!");
            }

            if (EmailDetails == null)
                EmailDetails = new Dictionary<string, Dictionary<string, string>>();
            if(!EmailDetails.ContainsKey(responderid) && emailDetail != null)
                EmailDetails.Add(responderid, emailDetail);
            
       

                
        private static bool RedirectionUrlValidationCallback(string redirectionUrl)
        {
            // The default for the validation callback is to reject the URL.
            bool result = false;

            Uri redirectionUri = new Uri(redirectionUrl);

            // Validate the contents of the redirection URL. In this simple validation
  相关解决方案