diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index fda16e7e..fd8d5ae4 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -201,42 +201,69 @@ public class ExchangeSession { HttpMethod initmethod = new GetMethod(url); wdr.executeHttpRequestMethod(httpClient, initmethod); + + // default destination is provided url + String destination = url; + if (!isBasicAuthentication) { LOGGER.debug("Form based authentication detected"); - if (initmethod.getPath().indexOf("exchweb/bin") == -1) { - LOGGER.error("DavMail configuration exception: authentication form not found at " + url + - " and basic authentication not requested"); - throw new IOException("DavMail configuration exception: authentication form not found at " + url + - " and basic authentication not requested"); - } else { - PostMethod logonMethod = new PostMethod( - "/exchweb/bin/auth/owaauth.dll?" + - "ForcedBasic=false&Basic=false&Private=true" + - "&Language=No_Value" - ); - logonMethod.addParameter("destination", url); - logonMethod.addParameter("flags", "4"); + // default logon method path + String logonMethodPath = "/exchweb/bin/auth/owaauth.dll"; + + // try to parse login form to determine logon url and destination + BufferedReader loginFormReader = null; + try { + loginFormReader = new BufferedReader(new InputStreamReader(initmethod.getResponseBodyAsStream())); + String line; + // skip to form action + final String FORM_ACTION = "