ews-java-api icon indicating copy to clipboard operation
ews-java-api copied to clipboard

EWS connection problem

Open nahiyanQ opened this issue 11 years ago • 7 comments

I am having problem with getting a connection to Exchange account. I am using office 365 plan E3.

Here is my code for connectVia autodiscover:

public static class RedirectionUrlCallback implements IAutodiscoverRedirectionUrl {
    public boolean autodiscoverRedirectionUrlValidationCallback(String redirectionUrl) {
        return redirectionUrl.toLowerCase().startsWith("https://");
  }
}

ExchangeService service = new ExchangeService();
service.setCredentials(new WebCredentials(<xxxx>@<company>.onmicrosoft.com, <password>));
service.autodiscoverUrl(email, new RedirectionUrlCallback());

Problem with this code is sometimes this code works and sometimes this doesnot work. When it doesnot work the exception I get is:

Jan 19, 2015 1:28:15 PM microsoft.exchange.webservices.data.AutodiscoverService tryGetEnabledEndpointsForHost
WARNING: Error while preparing asynchronous connection.
microsoft.exchange.webservices.data.EWSHttpException: Unable to open connection to https://<company>.onmicrosoft.com/autodiscover/autodiscover.xml
    at microsoft.exchange.webservices.data.HttpClientWebRequest.prepareAsyncConnection(HttpClientWebRequest.java:199)
    at microsoft.exchange.webservices.data.AutodiscoverService.tryGetEnabledEndpointsForHost(AutodiscoverService.java:1506)
    at microsoft.exchange.webservices.data.AutodiscoverService.tryGetAutodiscoverEndpointUrl(AutodiscoverService.java:1314)
    at microsoft.exchange.webservices.data.AutodiscoverService.getSettings(AutodiscoverService.java:1098)
    at microsoft.exchange.webservices.data.AutodiscoverService.getUserSettings(AutodiscoverService.java:1006)
    at microsoft.exchange.webservices.data.AutodiscoverService.internalGetSoapUserSettings(AutodiscoverService.java:949)
    at microsoft.exchange.webservices.data.AutodiscoverService.getUserSettings(AutodiscoverService.java:1836)
    at microsoft.exchange.webservices.data.ExchangeService.getAutodiscoverUrl(ExchangeService.java:3531)
    at microsoft.exchange.webservices.data.ExchangeService.autodiscoverUrl(ExchangeService.java:3458)
    at ews_managed_api.Ex01_FoldersSync.connectViaExchangeAutodiscover(Ex01_FoldersSync.java:48)
    at ews_managed_api.Ex01_FoldersSync.main(Ex01_FoldersSync.java:55)

Jan 19, 2015 1:28:16 PM microsoft.exchange.webservices.data.AutodiscoverService tryGetEnabledEndpointsForHost
WARNING: Error while preparing asynchronous connection.
microsoft.exchange.webservices.data.EWSHttpException: Unable to open connection to https://autodiscover.<company>.onmicrosoft.com/autodiscover/autodiscover.xml
    at microsoft.exchange.webservices.data.HttpClientWebRequest.prepareAsyncConnection(HttpClientWebRequest.java:199)
    at microsoft.exchange.webservices.data.AutodiscoverService.tryGetEnabledEndpointsForHost(AutodiscoverService.java:1506)
    at microsoft.exchange.webservices.data.AutodiscoverService.tryGetAutodiscoverEndpointUrl(AutodiscoverService.java:1314)
    at microsoft.exchange.webservices.data.AutodiscoverService.getSettings(AutodiscoverService.java:1098)
    at microsoft.exchange.webservices.data.AutodiscoverService.getUserSettings(AutodiscoverService.java:1006)
    at microsoft.exchange.webservices.data.AutodiscoverService.internalGetSoapUserSettings(AutodiscoverService.java:949)
    at microsoft.exchange.webservices.data.AutodiscoverService.getUserSettings(AutodiscoverService.java:1836)
    at microsoft.exchange.webservices.data.ExchangeService.getAutodiscoverUrl(ExchangeService.java:3531)
    at microsoft.exchange.webservices.data.ExchangeService.autodiscoverUrl(ExchangeService.java:3458)
    at ews_managed_api.Ex01_FoldersSync.connectViaExchangeAutodiscover(Ex01_FoldersSync.java:48)
    at ews_managed_api.Ex01_FoldersSync.main(Ex01_FoldersSync.java:55)

microsoft.exchange.webservices.data.AutodiscoverLocalException: The Autodiscover service didn't return an appropriate URL that can be used for the ExchangeService Autodiscover URL.
    at microsoft.exchange.webservices.data.ExchangeService.getEwsUrlFromResponse(ExchangeService.java:3589)
    at microsoft.exchange.webservices.data.ExchangeService.getAutodiscoverUrl(ExchangeService.java:3537)
    at microsoft.exchange.webservices.data.ExchangeService.autodiscoverUrl(ExchangeService.java:3489)
    at ews_managed_api.Ex01_FoldersSync.connectViaExchangeAutodiscover(Ex01_FoldersSync.java:48)
    at ews_managed_api.Ex01_FoldersSync.main(Ex01_FoldersSync.java:55)

When I loginto my account from https://login.microsoftonline.com/ I get url like this: https://pod51059.outlook.com/owa/#path=/mail

Here is my code for manual Url setting:

ExchangeService service = new ExchangeService();
ExchangeCredentials credentials =new WebCredentials(email, password);
service.setUrl(new URI("https://pod51059.outlook.com/ews/exchange.asmx"));
service.setCredentials(credentials);
service.setTraceEnabled(false);
Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
System.out.println("messages: " + inbox.getTotalCount());

This code(of manual url setting) was working fine even 3 days ago. But now getting exceptions like this:

microsoft.exchange.webservices.data.ServiceRequestException: The request failed. The request failed. The remote server returned an error: (403)Forbidden
    at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:58)
    at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:138)
    at microsoft.exchange.webservices.data.ExchangeService.bindToFolder(ExchangeService.java:317)
    at microsoft.exchange.webservices.data.ExchangeService.bindToFolder(ExchangeService.java:335)
    at microsoft.exchange.webservices.data.Folder.bind(Folder.java:46)
    at microsoft.exchange.webservices.data.Folder.bind(Folder.java:95)
    at ews_managed_api.Ex01_FoldersSync.connectViaExchangeManually(Ex01_FoldersSync.java:40)
    at ews_managed_api.Ex01_FoldersSync.main(Ex01_FoldersSync.java:57)
Caused by: microsoft.exchange.webservices.data.ServiceRequestException: The request failed. The remote server returned an error: (403)Forbidden
    at microsoft.exchange.webservices.data.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:630)
    at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:46)
    ... 7 more
Caused by: microsoft.exchange.webservices.data.HttpErrorException: The remote server returned an error: (403)Forbidden
    at microsoft.exchange.webservices.data.ServiceRequestBase.getEwsHttpWebResponse(ServiceRequestBase.java:682)
    at microsoft.exchange.webservices.data.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:625)
    ... 8 more

I have used the remote connectivity analyzer: https://testconnectivity.microsoft.com the connectivity test result was successful.

Now can anyone help me to solve this problem? Specially tell me why this sometimes works and most of the time does not work. I need a way that always works for setup connection.

Please also note that when I was able to get connection using these codes, I was able to do other kind of works like, retrieving mails, retrieving folder names, setting read flags.

nahiyanQ avatar Jan 19 '15 09:01 nahiyanQ

maybe try connecting via EWS Editor to test your account with the settings you mentioned. If this is working and you are facing errors with the api maybe we need to do some more investigation on this issue. Maybe you also can switch your tracing on for more feedback about requests and responses in ews-java-api.

serious6 avatar Jan 19 '15 20:01 serious6

Thanks André Behrens for your reply.

I tried the EWSEditor on my windows pc. I was able to connect both by "Autodiscover Email" and "Service URL" options.

In my java code I have turned on the trace as you said.

Today my "setting url manually" is working(in ews-java-api) But autodiscover is still not working and it is showing different exception than yesterday. Here is the exception today I am getting from autodiscover:

Jan 20, 2015 11:06:36 AM microsoft.exchange.webservices.data.AutodiscoverService tryGetEnabledEndpointsForHost
WARNING: Error while preparing asynchronous connection.
microsoft.exchange.webservices.data.EWSHttpException: Unable to open connection to https://<company>.onmicrosoft.com/autodiscover/autodiscover.xml
    at microsoft.exchange.webservices.data.HttpClientWebRequest.prepareAsyncConnection(HttpClientWebRequest.java:199)
    at microsoft.exchange.webservices.data.AutodiscoverService.tryGetEnabledEndpointsForHost(AutodiscoverService.java:1506)
    at microsoft.exchange.webservices.data.AutodiscoverService.tryGetAutodiscoverEndpointUrl(AutodiscoverService.java:1314)
    at microsoft.exchange.webservices.data.AutodiscoverService.getSettings(AutodiscoverService.java:1098)
    at microsoft.exchange.webservices.data.AutodiscoverService.getUserSettings(AutodiscoverService.java:1006)
    at microsoft.exchange.webservices.data.AutodiscoverService.internalGetSoapUserSettings(AutodiscoverService.java:949)
    at microsoft.exchange.webservices.data.AutodiscoverService.getUserSettings(AutodiscoverService.java:1836)
    at microsoft.exchange.webservices.data.ExchangeService.getAutodiscoverUrl(ExchangeService.java:3531)
    at microsoft.exchange.webservices.data.ExchangeService.autodiscoverUrl(ExchangeService.java:3458)
    at ews_managed_api.Ex00_ConnectionTest.connectViaExchangeAutodiscover(Ex00_ConnectionTest.java:36)
    at ews_managed_api.Ex00_ConnectionTest.main(Ex00_ConnectionTest.java:45)

Jan 20, 2015 11:06:37 AM microsoft.exchange.webservices.data.AutodiscoverService tryGetEnabledEndpointsForHost
WARNING: Error while preparing asynchronous connection.
microsoft.exchange.webservices.data.EWSHttpException: Unable to open connection to https://autodiscover.<company>.onmicrosoft.com/autodiscover/autodiscover.xml
    at microsoft.exchange.webservices.data.HttpClientWebRequest.prepareAsyncConnection(HttpClientWebRequest.java:199)
    at microsoft.exchange.webservices.data.AutodiscoverService.tryGetEnabledEndpointsForHost(AutodiscoverService.java:1506)
    at microsoft.exchange.webservices.data.AutodiscoverService.tryGetAutodiscoverEndpointUrl(AutodiscoverService.java:1314)
    at microsoft.exchange.webservices.data.AutodiscoverService.getSettings(AutodiscoverService.java:1098)
    at microsoft.exchange.webservices.data.AutodiscoverService.getUserSettings(AutodiscoverService.java:1006)
    at microsoft.exchange.webservices.data.AutodiscoverService.internalGetSoapUserSettings(AutodiscoverService.java:949)
    at microsoft.exchange.webservices.data.AutodiscoverService.getUserSettings(AutodiscoverService.java:1836)
    at microsoft.exchange.webservices.data.ExchangeService.getAutodiscoverUrl(ExchangeService.java:3531)
    at microsoft.exchange.webservices.data.ExchangeService.autodiscoverUrl(ExchangeService.java:3458)
    at ews_managed_api.Ex00_ConnectionTest.connectViaExchangeAutodiscover(Ex00_ConnectionTest.java:36)
    at ews_managed_api.Ex00_ConnectionTest.main(Ex00_ConnectionTest.java:45)

microsoft.exchange.webservices.data.ServiceRequestException: The request failed. The request failed. outlook.office365.com: unknown error
    at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:58)
    at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:138)
    at microsoft.exchange.webservices.data.ExchangeService.bindToFolder(ExchangeService.java:317)
    at microsoft.exchange.webservices.data.ExchangeService.bindToFolder(ExchangeService.java:335)
    at microsoft.exchange.webservices.data.Folder.bind(Folder.java:46)
    at microsoft.exchange.webservices.data.Folder.bind(Folder.java:95)
    at ews_managed_api.Ex00_ConnectionTest.connectViaExchangeAutodiscover(Ex00_ConnectionTest.java:38)
    at ews_managed_api.Ex00_ConnectionTest.main(Ex00_ConnectionTest.java:45)
Caused by: microsoft.exchange.webservices.data.ServiceRequestException: The request failed. outlook.office365.com: unknown error
    at microsoft.exchange.webservices.data.ServiceRequestBase.getEwsHttpWebResponse(ServiceRequestBase.java:686)
    at microsoft.exchange.webservices.data.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:625)
    at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:46)
    ... 7 more
Caused by: java.net.UnknownHostException: outlook.office365.com: unknown error
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:907)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1302)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1255)
    at java.net.InetAddress.getAllByName(InetAddress.java:1171)
    at java.net.InetAddress.getAllByName(InetAddress.java:1105)
    at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:101)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at microsoft.exchange.webservices.data.HttpClientWebRequest.executeRequest(HttpClientWebRequest.java:368)
    at microsoft.exchange.webservices.data.ServiceRequestBase.getEwsHttpWebResponse(ServiceRequestBase.java:677)
    ... 9 more

Here is my complete code that Im using for testing the connections.

package ews_managed_api;

import java.net.URI;

import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.Folder;
import microsoft.exchange.webservices.data.IAutodiscoverRedirectionUrl;
import microsoft.exchange.webservices.data.WebCredentials;
import microsoft.exchange.webservices.data.WellKnownFolderName;

public class Ex00_ConnectionTest {
  public static class RedirectionUrlCallback implements IAutodiscoverRedirectionUrl {
    public boolean autodiscoverRedirectionUrlValidationCallback(String redirectionUrl) {
      return redirectionUrl.toLowerCase().startsWith("https://");
    }
  }

  public static ExchangeService connectViaExchangeManually(String email, String password)
      throws Exception {
    ExchangeService service = new ExchangeService();
    ExchangeCredentials credentials = new WebCredentials(email, password);
    service.setUrl(new URI("https://pod51059.outlook.com/ews/exchange.asmx"));
    service.setCredentials(credentials);
    service.setTraceEnabled(true);
    Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
    System.out.println("messages: " + inbox.getTotalCount());
    return service;
  }

  public static ExchangeService connectViaExchangeAutodiscover(String email, String password)
      throws Exception {
    ExchangeService service = new ExchangeService();
    service.setCredentials(new WebCredentials(email, password));
    service.autodiscoverUrl(email, new RedirectionUrlCallback());
    service.setTraceEnabled(true);
    Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
    System.out.println("messages: " + inbox.getTotalCount());
    return service;
  }

  public static void main(String[] args) {
    try {
      ExchangeService service = connectViaExchangeAutodiscover("<name>@<company>.onmicrosoft.com", "<password>");
      //ExchangeService service = connectViaExchangeManually("<name>@<company>.onmicrosoft.com", "<password>");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

I am working on ubuntu 14.04.1

nahiyanQ avatar Jan 20 '15 05:01 nahiyanQ

Okay I have just tested the code in another pc and everything is working. I guess the problem was only with my pc.

nahiyanQ avatar Jan 20 '15 06:01 nahiyanQ

Hi, I need to develop a feature to scan emails from outlook public folders. So I tried with below code segment with ews-java-api but for Inbox it works with no issues. When I try to access mail from public folders it generates exception saying "There are no public folder servers available."

Code : I have maintain my credentials in property file Exchange service URL we are using is https://hostname/EWS/Exchange.asmx

public static void main(String[] args) { String serviceUrl = ""; try { //Load Property Properties prop = new Properties(); prop.load(new FileInputStream("config.properties"));

		//Get exchange service object		
		ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
		
		String mailId = prop.getProperty("mailId");
		String passWd = prop.getProperty("mailPwd");
		serviceUrl = prop.getProperty("serviceUrl");
		
		//System.out.println("mailId = "+mailId+"\t pwd = "+passWd+"\t serviceUrl = "+serviceUrl);
			
		//Set credentials
		ExchangeCredentials credentials = new WebCredentials(mailId, passWd);
			
		//Pass credentials to service
		service.setCredentials(credentials);
			
		//Set service Url
		service.setUrl(new URI(serviceUrl.trim()));
			
		FolderView view = new FolderView(5);
			
		FindFoldersResults res = service.findFolders(WellKnownFolderName.PublicFoldersRoot, view);
			
		for(Folder f : res.getFolders()){
				System.out.println("Name : "+f.getDisplayName());
		}
			
	}catch(Exception e){
		e.printStackTrace();			
	}

}

ERROR : microsoft.exchange.webservices.data.ServiceResponseException: There are no public folder servers available. at microsoft.exchange.webservices.data.ServiceResponse.internalThrowIfNecessary(Unknown Source) at microsoft.exchange.webservices.data.ServiceResponse.throwIfNecessary(Unknown Source) at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(Unknown Source) at microsoft.exchange.webservices.data.ExchangeService.internalFindFolders(Unknown Source) at microsoft.exchange.webservices.data.ExchangeService.findFolders(Unknown Source) at microsoft.exchange.webservices.data.ExchangeService.findFolders(Unknown Source) at com.wellmanage.fi.ReadFromPublic.main(ReadFromPublic.java:62)

What could be the reason for this? Please help. Thanks

yatinc1206 avatar Apr 07 '17 13:04 yatinc1206

Hi, Does anybody know solution to this?

yatinc1206 avatar Apr 11 '17 07:04 yatinc1206

Try send emails first ?

lycenok avatar May 08 '20 09:05 lycenok

@yatinc1206 login to your outlook with the same credentials you happen to use in code. check there if any public folders are available. left click at the top left small arrow,sidebar will open. you will find it there

Kishangalgotra avatar Aug 05 '21 07:08 Kishangalgotra