Java EWS API - reading non default mailbox

5.4k Views Asked by At

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!

2

There are 2 best solutions below

4
On

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

ExchangeServiceBinding binding = new ExchangeServiceBinding();
// Use this class instead of ExchangeService (!)
binding.Credentials = credentials;  
// etc    

binding.ExchangeImpersonation = new ExchangeImpersonationType();
binding.ExchangeImpersonation.ConnectingSID = new ConnectingSIDType();
binding.ExchangeImpersonation.ConnectingSID.PrimarySmtpAddress = "<mailbox_address>";

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:

you have to set to rights “ms-Exch-EPI-Impersonation” must be on the Client Access server for the service account which will do the impersonation. Additionally the service account will have to have the “ms-Exch-EPI-May-Impersonate” right granted to it on each mailbox it will impersonate. This right is inherited down to mailboxes if you were to set it at the store, storage group, server, or organizational level.

0
On

This code seems to be correct and if this is working for your default mailbox, it could be some permission error.

I am using similar code to access mails of shared mail-groups

Only difference I see is -> WellKnownFolderName.MsgFolderRoot instead of WellKnownFolderName.Root