I would like to read a non default mailbox on an exchange server with the Java EWS API, but something is wrong with my code. Here is an excerpt with the relevant part:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
ExchangeCredentials credentials = new WebCredentials("<user>", "<pass>");
service.setCredentials(credentials);
service.setUrl(new URI("https://<URL>/EWS/Exchange.asmx"));
ItemView iview = new ItemView(3);
Mailbox mb = new Mailbox();
mb.setAddress("<mailbox_address>");
FolderId folderId = new FolderId(WellKnownFolderName.Root, mb);
FindItemsResults<Item> findResults = service.findItems(folderId, iview);
And the error message is:
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.getResponseHeaders(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)
Btw. I am able to read my default mailbox, send emails, etc...
Could you please advise? Thanks in advance!
I believe you are trying to access one mailbox, but using the credentials of another account. This can be solved by impersonation.
In newer versions of the EWS this can either be solved in AD on the exchange server or by programmatically setting the ConnectingSID property. I would probably bank on the second one and make the primary SMTP address configurable for different environments.
Here is a code example using EWS 2010 in C#, so you might have to use
setCredentials(credentials)
instead and so on:Code example EWS 2010
Documentation for EWS 2007
It seems like this should be available also in EWS 2007 SP1, according to this article. Please note, from the article, that: