exchangewebservices - 将EWS与Office 365一起使用时由于服务器不支持请求的版本而失败

标签 exchangewebservices office365

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

    }
}

最佳答案

如果我更改行:

ExchangeService service = new ExchangeService();


ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

该代码工作正常。我不明白为什么要在此调用中显式传递版本;为什么?为什么没有特定版本会失败?

关于exchangewebservices - 将EWS与Office 365一起使用时由于服务器不支持请求的版本而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16348041/

相关文章:

python - 在python中的azure databricks笔记本中发送电子邮件(outlook作为服务器)?

azure - 组织帐户 (office 365) 可以用于实时/Microsoft 服务吗?

api - 如何从 Office 365 REST API 检索 ItemAttachment 内容?

php - 使用 php-ews 创建定期日历事件

python - O365 EO 可恢复项目文件夹的 REST Api

email - Exchange 2010/13 - 按类别搜索邮件并将其设置为敏感度 :private

excel - 针对旧版本的 excel 测试 office-js 插件

ios - Exchange Web 服务 : Get a list of Rooms for a particular Time window

asp.net - EWS Managed Api 中的电子邮件对话/电子邮件线程支持(针对 Exchange 2010 左右)

asp.net-mvc - MVC 对 Office 365 AD 进行身份验证