title: Could not establish the connection to the Exchange Web Service - Java API
body: <p>I am testing the <strong>Microsoft Exchange Web Service Java API</strong> (version 1.2) to read mails from a server. Here is my code:</p>
  
  <pre><code>String url = "https://my-server/EWS/exchange.asmx";
  ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
  service.setTraceEnabled(true);
  service.setCredentials(new WebCredentials("user", "password"));
  service.setUrl(url.toURI());
  
  Mailbox mailbox = new Mailbox("foo@bar.com");
  FolderId folder = new FolderId(WellKnownFolderName.Inbox, mailbox);
  ItemView view = new ItemView(10);
  view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending);
  FindItemsResults&lt;Item&gt; items = service.findItems(folder, view);
  </code></pre>
  
  <p>Unfortunately, this code throws the following error:</p>
  
  <pre><code>Exception in thread "main" microsoft.exchange.webservices.data.EWSHttpException: Connection not established
      at microsoft.exchange.webservices.data.HttpClientWebRequest.throwIfConnIsNull(Unknown Source)
      at microsoft.exchange.webservices.data.HttpClientWebRequest.getResponseCode(Unknown Source)
      at microsoft.exchange.webservices.data.EwsUtilities.formatHttpResponseHeaders(Unknown Source)
      at microsoft.exchange.webservices.data.ExchangeServiceBase.traceHttpResponseHeaders(Unknown Source)
      at microsoft.exchange.webservices.data.ExchangeServiceBase.processHttpResponseHeaders(Unknown Source)
      at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(Unknown Source)
      at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(Unknown Source)
      at microsoft.exchange.webservices.data.ExchangeService.findItems(Unknown Source)
      at microsoft.exchange.webservices.data.ExchangeService.findItems(Unknown Source)
      at foo.bar.TestMail.main(TestMail.java:52)
  </code></pre>
  
  <p>I'm not able to understand what is wrong with the current code. Do you have any clue, or at least some tests to try?</p>
  
  <p>Note that the web-service (<code>https//my-server/exchange.asmx</code>) is accessible to my Java code.</p>
  
  <p>Not sure if it can help, but I've found that in the traces displayed by the logs:</p>
  
  <pre><code>&lt;Trace Tag="EwsResponse" Tid="1" Time="2013-01-28 10:47:03Z"&gt;
  &lt;html&gt;&lt;head&gt;&lt;title&gt;Error&lt;/title&gt;&lt;/head&gt;&lt;body&gt;The function requested is not supported
  &lt;/body&gt;&lt;/html&gt;
  &lt;/Trace&gt;
  </code></pre>
  
  <hr/>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've made another test. This time, I use the defaults credentials - so my own email account - using <code>service.setUseDefaultCredentials(true);</code> instead of setting the <code>WebCredentials</code> object. The connection is still not established, but I get another error (I've took only the interesting part of the trace log):</p>
  
  <pre><code>&lt;h1&gt;You are not authorized to view this page&lt;/h1&gt;
  You do not have permission to view this directory or page using the credentials that you supplied because your Web browser is sending a WWW-Authenticate header field that the Web server is not configured to accept.
  (...)
  &lt;h2&gt;HTTP Error 401.2 - Unauthorized: Access is denied due to server configuration.&lt;br&gt;Internet Information Services (IIS)&lt;/h2&gt;
  </code></pre>
  
  <p>Of course, I can't access nor change anything on the Exchange server side. Is there a way to make the authentication successful?</p>