6

使用 Windows 8(64 位)和 Visual Studio 2012 Ultimate,更新 2。

我正在尝试将 Exchange Web Services 2.0 (EWS) 与 Office 365 一起使用。在调用 findResults 时出现以下异常(main 中的最后一行):

Microsoft.Exchange.WebServices.Data.ServiceVersionException was unhandled
  HResult=-2146233088
  Message=Exchange Server doesn't support the requested version.
  Source=Microsoft.Exchange.WebServices
  StackTrace:
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ProcessWebException(WebException webException)
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request)
       at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.InternalExecute()
       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, ViewBase view)
       at Test_EWS.Program.Main(String[] args) in c:\Users\john.tarbox\Documents\Visual Studio 2012\Projects\Test_EWS\Test_EWS\Program.cs:line 85
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

这是我的代码示例:

using Microsoft.Exchange.WebServices.Autodiscover;
using Microsoft.Exchange.WebServices.Data;
using System;
using System.Net;


namespace Test_EWS
{
    class Program
    {
        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))
                        {
                            // Self-signed certificates with an untrusted root are valid. 
                            continue;
                        }
                        else
                        {
                            if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
                            {
                                // If there are any other errors in the certificate chain, the certificate is invalid,
                                // so the method returns false.
                                return false;
                            }
                        }
                    }
                }

                // When processing reaches this line, the only errors in the certificate chain are 
                // untrusted root errors for self-signed certificates. These certificates are valid
                // for default Exchange server installations, so return true.
                return true;
            }
            else
            {
                // In all other cases, return false.
                return false;
            }
        }


        static void Main(string[] args)
        {
            ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
            ExchangeService service = new ExchangeService();
            service.Credentials = new WebCredentials("xxx@yyy.com", "password");

            service.TraceEnabled = true;
            service.TraceFlags = TraceFlags.All;

            try
            {
                service.AutodiscoverUrl("xxx@yyy.com", RedirectionUrlValidationCallback);
            }
            catch (AutodiscoverRemoteException ex)
            {                
                Console.WriteLine("Exception thrown: " + ex.Error.Message);

         }

            FindItemsResults<Item> findResults = service.FindItems(
                                       WellKnownFolderName.Inbox,
                                       new ItemView(10));
        }

        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;
}

    }
}
4

2 回答 2

12

如果我换行:

ExchangeService service = new ExchangeService();

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

代码工作得很好。我不明白为什么需要在此调用中明确传递版本;为什么没有特定版本会失败?

于 2013-05-02T21:55:50.753 回答
0

我有同样的问题VS2010。Outlook 2013 附带不支持 Excnage2007_SP1 的 EWS V15 (GAC\Microsoft.Exchange.WebServices\15.0.0.0_...)。

请改用 ExchangeVersion.Exchange2010_SP2。它解决了我的问题。

于 2013-05-20T20:57:07.450 回答