6
votes

Using Windows 8 (64 bit) and Visual Studio 2012 Ultimate, Update 2.

I am trying to use Exchange Web Services 2.0 (EWS) with Office 365. I am getting the following exception which occurs on the call to findResults (last line in 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: 

Here is my code sample:

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("[email protected]", "password");

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

            try
            {
                service.AutodiscoverUrl("[email protected]", 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;
}

    }
}
2

2 Answers

12
votes

If I change the line:

ExchangeService service = new ExchangeService();

to

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

the code works just fine. I do not understand why one should need to pass the version explicitly on this call; why does it fail without a specific version?

0
votes

I had the same issue VS2010. With Outlook 2013 comes EWS V15 (GAC\Microsoft.Exchange.WebServices\15.0.0.0_...) which does not Support Excnage2007_SP1.

Try ExchangeVersion.Exchange2010_SP2 instead. It solved my Problem.