none
EWS JAVA API - 1.3 - cannot authenticate NTLM RRS feed

  • Question

  • Just some line to report my experience with the library EWS JAVA API (ews-java-api found at github).


    I was trying to send email from my java test application via the EWS service published by an Exchange Server with only NTLM authentication enabled.

    The first tries were all unsuccessful, resulting in a authentication problem.

    In the end I found the post 14702764 at stackoverflow (how-to-use-ldap-authentication-for-the-exchange-web-services-connection-in-java) that solved my problem.


    Once I changed the inner class NTLM inside EwsJCIFSNTLMScheme.java as described in the link above, the problem of authentication via NTLM disappeared.

    Following is the error occuring before I changed the libary code:

    2014-09-21 01:19:48,818 INFO  [AuthChallengeProcessor] - NTLM authentication scheme selected

    2014-09-21 01:19:48,940 INFO  [HttpMethodDirector] - Failure authenticating with NTLM <any realm>@test.domail.com:443

    Exception in thread "main" microsoft.exchange.webservices.data.ServiceRequestException: The request failed. null
    at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:63)
    at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:142)
    at microsoft.exchange.webservices.data.ExchangeService.internalCreateItems(ExchangeService.java:464)
    at microsoft.exchange.webservices.data.ExchangeService.createItem(ExchangeService.java:535)

    at microsoft.exchange.webservices.data.Item.internalCreate(Item.java:215)
    at microsoft.exchange.webservices.data.EmailMessage.internalSend(EmailMessage.java:125)
    at microsoft.exchange.webservices.data.EmailMessage.send(EmailMessage.java:253)
    at com.vodafone.spp.test.testews.Main.main(Main.java:40)
    Caused by: java.lang.NullPointerException
    at microsoft.exchange.webservices.data.SimpleServiceRequestBase.readResponse(SimpleServiceRequestBase.java:141)
    at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:50)
    ... 7 more

    This is the code fragment to replace in file EwsJCIFSNTLMScheme.java :

        private class NTLM {
        /** Character encoding */
        public static final String DEFAULT_CHARSET = "ASCII";

        /**
        * The character was used by 3.x's NTLM to encode the username and
        * password. Apparently, this is not needed in when passing username,
        * password from NTCredentials to the JCIFS library
        */
        private String credentialCharset = DEFAULT_CHARSET;

        void setCredentialCharset(String credentialCharset) {
               this.credentialCharset = credentialCharset;
        }

        private static final int TYPE_1_FLAGS = NtlmFlags.NTLMSSP_NEGOTIATE_NTLM
                     | NtlmFlags.NTLMSSP_NEGOTIATE_UNICODE
                     | NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2;

        private String generateType1Msg(String host, String domain) {
               jcifs.ntlmssp.Type1Message t1m = new jcifs.ntlmssp.Type1Message(
                            TYPE_1_FLAGS, domain, host);
               return jcifs.util.Base64.encode(t1m.toByteArray());
        }

        private String generateType3Msg(String username, String password,
                     String host, String domain, String challenge) {
               jcifs.ntlmssp.Type2Message t2m;
               try {
                     t2m = new jcifs.ntlmssp.Type2Message(
                                   jcifs.util.Base64.decode(challenge));
               } catch (IOException e) {
                     throw new RuntimeException("Invalid Type2 message", e);
               }

               final int type2Flags = t2m.getFlags();
               final int type3Flags = type2Flags
                            & (0xffffffff ^ (NtlmFlags.NTLMSSP_TARGET_TYPE_DOMAIN | NtlmFlags.NTLMSSP_TARGET_TYPE_SERVER));

               jcifs.ntlmssp.Type3Message t3m = new jcifs.ntlmssp.Type3Message(
                            t2m, password, domain, username, host, type3Flags);
               return jcifs.util.Base64.encode(t3m.toByteArray());
        }



    Sunday, September 21, 2014 3:46 AM

All replies

  • Hi Andrea

    Did you update to  the latest version of java ews api and see the results ?

    However after changing the inner class NTLM inside as mentioned 14702764  solved your issue. We need to check if the latest version of ews api has this value corrected.

    Thanks a lot  for posting the solution as well :)


    Remember to mark as helpful if you find my contribution useful or as an answer if it does answer your question.That will encourage me - and others - to take time out to help you Check out my latest blog posts on http://exchangequery.com

    Sunday, September 21, 2014 9:54 AM
  • Hi Andrea,

    I am writing a email client for downloading emails from Microsoft exchange server 2010. I am also using ews-java API. Getting the same error. Is is possible for you to share the client and code and class file of the java file you patched above. 

    Thanks in advance.

    Thursday, January 22, 2015 6:12 PM