I try to write a Java application that access an Exchange Web Services in order to read emails. Thus, I use the Exchange Web Services (EWS
) Java API provided by Microsoft.
I already had several issues with it, and I finally found that the authentication should be done using LDAP. Unfortunately, I'm not sure how to do such a thing. Does the EWS API allows to configure the authentication scheme to be used when connecting to the Exchange server ? If yes, how to configure that?
This is the code I use for connection, but it uses the default authentication scheme, i.e. NTLM
:
String url = "https//my-server/EWS/exchange.asmx";
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.setTraceEnabled(true);
service.setCredentials(new WebCredentials("user", "password"));
service.setUrl(url.toURI());
Mailbox mailbox = new Mailbox("foo@bar.com");
FolderId folder = new FolderId(WellKnownFolderName.Inbox, mailbox);
ItemView view = new ItemView(10);
view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending);
FindItemsResults<Item> items = service.findItems(folder, view);
We had the same issue and although changing the NTLM class (as romaintaz suggested) works, it was breaking at some other point.
However, there is a newer version of the EWS Java library, hostet at github: https://github.com/OfficeDev/ews-java-api
It now uses apache httpclient 4.4.1, which has a good NTLM-implementation.
Using this library, we had no more issues regarding NTLM authentication so far.
We resolved this issue. In fact, we had 2 solutions for that:
In the Microsft EWS API, the class
NTLM
was wrong. So we re-built the JAR with the following code for the class:Another solution is to use the JWebServices library (commercial).