this code can excuted success on my local machine, but got this error when I ran this on the test running client. below is my code:
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack; ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); service.Credentials = new NetworkCredential("testaccount", "passwd", "domain"); service.AutodiscoverUrl("testaccount@company.com", 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 // callback, the redirection URL is considered valid if it is using HTTPS // to encrypt the authentication credentials. if (redirectionUri.Scheme == "https") { result = true; } return result; } private static bool CertificateValidationCallBack(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { // If the certificate is a valid, signed certificate, return true. if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None) { return true; } // If there are errors in the certificate chain, look at each error to determine the cause. if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0) { if (chain != null && chain.ChainStatus != null) { foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus) { if ((certificate.Subject == certificate.Issuer) && (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot)) { continue; } else { if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError) { return false; } } } } return true; } else { return false; } }
below is the exception:
Exception when trying to execute test : Microsoft.Exchange.WebServices.Data.ServiceRequestException: The request failed. Unable to connect to the remote server ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.184.69.27:443 at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context) at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult) at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.EmitRequest(IEwsHttpWebRequest request) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.BuildEwsHttpWebRequest() --- End of inner exception stack trace --- at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.BuildEwsHttpWebRequest() at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request) at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute() at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems[TItem](IEnumerable`1 parentFolderIds, SearchFilter searchFilter, String queryString, ViewBase view, Grouping groupBy, ServiceErrorHandling errorHandlingMode) at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(FolderId parentFolderId, SearchFilter searchFilter, ViewBase view) at Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(WellKnownFolderName parentFolderName, SearchFilter searchFilter, ViewBase view) at ........