Search code examples
javaexchangewebservicesexchange-server-2010ewsjavaapi

Set authentication to Basic in EWS Java API code to send mails


I tried to use the following code to send mail using EWS Java API. But I get the error that the NTLM Authentication is selected. I need only Basic Authentication. How do I specify the authentication type.

    ExchangeCredentials credentials = new WebCredentials("user", "pwd");
    service.setCredentials(credentials);
    try {
        service.setUrl(new java.net.URI("https://url/EWS/Exchange.asmx"));
        service.setTraceEnabled(true);

        EmailMessage msg = new EmailMessage(service);
        msg.setSubject("Hello world!");
        msg.setBody(MessageBody
                .getMessageBodyFromText("Sent using the EWS Managed API."));
        msg.getToRecipients().add("email");
        msg.send();
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Please tell me how to solve this....:(


Solution

  • It depends on the configuration of you exchange server. Try using the different usernames who can be configured on exchange servers:

    ExchangeService service = new ExchangeService();
    
    ExchangeCredentials credentials = new WebCredentials("[email protected]","pass");
    

    or

    ExchangeCredentials credentials = new WebCredentials(Username,Password, domain);
    

    or

    ExchangeCredentials credentials = new WebCredentials("user", "pwd");
    
    service.setCredentials(credentials);
    

    In my case, the one who works is the Username,Password,Domain method.