关于网友提出的“用ExchangeService取邮件错误The request failed Unable to connect to the remote server”问题疑问,本网通过在网上对“用ExchangeService取邮件错误The request failed Unable to connect to the remote server”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:用ExchangeService取邮件错误The request failed Unable to connect to the remote server
描述:
exchangeservice邮件C#WebService
我调用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("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- 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();
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<>>();
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;
}
}
在测试机器上跑出的异常是:
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 ........
解决方案1:
错误提示很清楚,不能连接10.184.69.27:443,楼主查询配置文件是否配置不正确,或者ping一下
以上介绍了“用ExchangeService取邮件错误The request failed Unable to connect to the remote server”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/951667.html