title: EWS microsoft.exchange.webservices.data.ServiceLocalException Connection error
body: <p>Getting microsoft.exchange.webservices.data.ServiceLocalException: <a href="https://outlook.xxxxxxxxx.com/ews/Exchange.asmx" rel="nofollow">https://outlook.xxxxxxxxx.com/ews/Exchange.asmx</a> : Connection error </p>
  
  <p>While trying to access exchange server using EWS JAVA API.</p>
  
  <p>Already have EWSJavaAPI_1.2.1.jar, EWSJavaAPI_1.2original.jar, EWSJavaAPIWithJars_1.2.1.jar, httpclient-4.2.5.jar, httpcore-4.2.4.jar, jcifs-1.3.17.jar, commons-codec-1.7.jar, commons-logging-1.1.1.jar in classpath.</p>
  
  <p>Here is the detailed error</p>
  
  <pre><code>microsoft.exchange.webservices.data.ServiceLocalException: https://outlook.xxxxxxxxx.com/ews/Exchange.asmx : Connection error 
      at microsoft.exchange.webservices.data.ExchangeServiceBase.prepareHttpWebRequestForUrl(Unknown Source)
      at microsoft.exchange.webservices.data.ExchangeService.prepareHttpWebRequest(Unknown Source)
      at microsoft.exchange.webservices.data.ServiceRequestBase.buildEwsHttpWebRequest(Unknown Source)
      at microsoft.exchange.webservices.data.ServiceRequestBase.validateAndEmitRequest(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.bindToFolder(Unknown Source)
      at microsoft.exchange.webservices.data.ExchangeService.bindToFolder(Unknown Source)
      at microsoft.exchange.webservices.data.Folder.bind(Unknown Source)
      at microsoft.exchange.webservices.data.Folder.bind(Unknown Source)
      at com.xxxxxxx.action.MSExchangeEmailService.readEmails(MSExchangeEmailService.java:105)
      at com.xxxxxxx.action.MSExchangeEmailService.main(MSExchangeEmailService.java:47)
  </code></pre>
  
  <p>Here is the sample code below</p>
  
  <pre><code>public static void main(String[] args) {
  MSExchangeEmailService msees = new MSExchangeEmailService();
  msees.readEmails();
  //msees.readAppointments();
  }
  
  static{
  try{
  service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
  //service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); //depending on the version of your Exchange. 
  service.setUrl(new URI("https://outlook.xxxxxxxxx.com/ews/Exchange.asmx"));
  }catch (Exception e) {
  e.printStackTrace();
  }
  }
  
  
  public MSExchangeEmailService() {
  ExchangeCredentials credentials = new WebCredentials("xxxx@xxxxx.com","xxxx","xxxxx");
  service.setCredentials(credentials);
  service.setTraceEnabled( true );
  }
  
  public List readEmails(){
  List msgDataList = new ArrayList();
  try{
  Folder folder = Folder.bind( service, WellKnownFolderName.Inbox );
  FindItemsResults&lt;Item&gt; results = service.findItems(folder.getId(), new ItemView(NUMBER_EMAILS_FETCH));
  int i =1;
  for (Item item : results){
  Map messageData = new HashMap();
  messageData = readEmailItem(item.getId());
  System.out.println("\nEmails #" + (i++ ) + ":" );
  System.out.println("subject : " + messageData.get("subject").toString());
  System.out.println("Sender : " + messageData.get("senderName").toString());
  msgDataList.add(messageData);
  }
  }catch (Exception e) { e.printStackTrace();}
  return msgDataList;
  }
  </code></pre>