用ExchangeService取邮件错误The request failed. Unable to connect to the remote server

hualibinbin 2014-02-12 04:43:05
我调用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<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;
}
}



在测试机器上跑出的异常是:

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 ........
...全文
2478 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
java守门员 2015-09-15
  • 打赏
  • 举报
回复
怎么解决的啊
风一样的大叔 2014-02-18
  • 打赏
  • 举报
回复
是不是远程服务器防火墙的问题,或者网管上做了什么限制
hualibinbin 2014-02-13
  • 打赏
  • 举报
回复
木有配置文件,在所跑的测试机上能Ping 通 但是 不能 远程 connect 到指定 server
风一样的大叔 2014-02-13
  • 打赏
  • 举报
回复
错误提示很清楚,不能连接10.184.69.27:443,楼主查询配置文件是否配置不正确,或者ping一下

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧