1

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!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
bayerb
  • 649
  • 2
  • 9
  • 28
  • Have you tried the overload of the `WebCredentials` constructor with 3 arguments? Perhaps you are missing the domain. – Halvard Aug 13 '13 at 09:57
  • Have you seen this question: http://stackoverflow.com/questions/14562153/could-not-establish-the-connection-to-the-exchange-web-service-java-api ? Seems there are several very similar questions on StackOverflow and some of these have to do with which authentication the Exchange Server is configured with. Don't know if I can help you more as I can't see anything wrong with your code. – Halvard Aug 13 '13 at 10:19
  • Actually I am able to authenticate. e.g. I can access my default mailbox. Only if I try to access a non default mailbox, I get the error message above. – bayerb Aug 13 '13 at 11:31
  • Ah, I missed that you had written that in your question. I have another question to see if I understand this correctly: Your credentials work for one email address, but not for another? Have you tried giving the credentials for the other mailbox (or are they the same)? Usually you have to use some kind of impersonation to look into other email accounts, I would think. – Halvard Aug 13 '13 at 13:31
  • Yes, the credentials are the same for bopth my default, and non default mailboxes. – bayerb Aug 13 '13 at 14:42
  • I would still try to see if the impersonation described in my answer is available for you, but I am not so sure it will help after all. – Halvard Aug 13 '13 at 15:15

2 Answers2

0

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.

Halvard
  • 3,891
  • 6
  • 39
  • 51
  • How can I set up this connectingSID property with this API? – bayerb Aug 13 '13 at 14:45
  • I added the line: service.setImpersonatedUserId(new ImpersonatedUserId(ConnectingIdType.SmtpAddress, mailbox_address)); Still the same error. – bayerb Aug 14 '13 at 08:08
  • @bayerb I have one last step for you: Make sure the user you are using for impersonation is configured on the server like this: http://msdn.microsoft.com/en-us/library/bb204095.aspx I hope this helps! If not, I am out of suggestions :( – Halvard Aug 14 '13 at 08:15
  • Unfortunately I do not have any influence on the server configuration. I already tried it with python-suds, c++-gsoap, and this is the 3th unsuccessful try. VB and c# is not good, because the software needs to run on linux. Anyway, thanks for your help. – bayerb Aug 18 '13 at 10:30
0

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

DhruvG
  • 394
  • 1
  • 2
  • 10