Romain Linsolas
Romain Linsolas

Reputation: 81617

Could not establish the connection to the Exchange Web Service - Java API

I am testing the Microsoft Exchange Web Service Java API (version 1.2) to read mails from a server. Here is my code:

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("[email protected]");
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);

Unfortunately, this code throws the following error:

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.getResponseCode(Unknown Source)
    at microsoft.exchange.webservices.data.EwsUtilities.formatHttpResponseHeaders(Unknown Source)
    at microsoft.exchange.webservices.data.ExchangeServiceBase.traceHttpResponseHeaders(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)
    at foo.bar.TestMail.main(TestMail.java:52)

I'm not able to understand what is wrong with the current code. Do you have any clue, or at least some tests to try?

Note that the web-service (https//my-server/exchange.asmx) is accessible to my Java code.

Not sure if it can help, but I've found that in the traces displayed by the logs:

<Trace Tag="EwsResponse" Tid="1" Time="2013-01-28 10:47:03Z">
<html><head><title>Error</title></head><body>The function requested is not supported
</body></html>
</Trace>

EDIT

I've made another test. This time, I use the defaults credentials - so my own email account - using service.setUseDefaultCredentials(true); instead of setting the WebCredentials object. The connection is still not established, but I get another error (I've took only the interesting part of the trace log):

<h1>You are not authorized to view this page</h1>
You do not have permission to view this directory or page using the credentials that you supplied because your Web browser is sending a WWW-Authenticate header field that the Web server is not configured to accept.
(...)
<h2>HTTP Error 401.2 - Unauthorized: Access is denied due to server configuration.<br>Internet Information Services (IIS)</h2>

Of course, I can't access nor change anything on the Exchange server side. Is there a way to make the authentication successful?

Upvotes: 5

Views: 10018

Answers (3)

nik_7
nik_7

Reputation: 137

This is an Example of EWS JAVA api 1.2,This is one of the ways you can try

package com.ea.connector.exchange;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import microsoft.exchange.webservices.data.BasePropertySet;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.FindItemsResults;
import microsoft.exchange.webservices.data.Item;
 import microsoft.exchange.webservices.data.ItemSchema;
import microsoft.exchange.webservices.data.ItemView;
import microsoft.exchange.webservices.data.LogicalOperator;
import microsoft.exchange.webservices.data.OffsetBasePoint;
import microsoft.exchange.webservices.data.PropertySet;
import microsoft.exchange.webservices.data.SearchFilter;
import microsoft.exchange.webservices.data.SortDirection;
import microsoft.exchange.webservices.data.WebCredentials;
import microsoft.exchange.webservices.data.WebProxy;
import microsoft.exchange.webservices.data.WellKnownFolderName;

import org.apache.commons.httpclient.NTCredentials;

import com.ea.connector.exchange.bean.ExchangeConnectionParamBean;
import com.ea.connector.exchange.util.ExchMessageProperties;
import com.ea.connector.net.ExchGetItemRequest;

public class Test {

protected static ArrayList<String> propertiesToFetch = new ArrayList<String>();

private static ExchangeService mService;

 // private Settings mSettings;
//  
//  private int imailCount;

private Date dStartDate;
private Date dEndDate;
private static ExchangeConnectionParamBean objParamBean;
public void setPropertiesToBeFetched(List<String> filter) {
    propertiesToFetch.addAll(filter);
}
public Date getConnectorStartDate() {
    return dStartDate;
}

public void setConnectorStartDate(Date dStartDate) {
    this.dStartDate = dStartDate;
}

public ExchangeConnectionParamBean getParamBean() {
    return objParamBean;
}

public void setParambean(ExchangeConnectionParamBean objParambean) {
    Test.objParamBean = objParambean;
}

public Date getConnectorEndDate() {
    return dEndDate;
}

public void setConnectorEndDate(Date dEndDate) {
    this.dEndDate = dEndDate;
}


    public static void main(String []args) throws URISyntaxException,Exception
    {
        setCredentialsAndGetExchRequest();
        System.out.println("conncection established");
    }

    protected static ExchGetItemRequest setCredentialsAndGetExchRequest() throws   URISyntaxException,Exception{

        @SuppressWarnings("unused")
        FindItemsResults<Item> resultMails;

        mService = new ExchangeService();
        mService.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx"));
        WebProxy paramWebProxy=new WebProxy("ptbproxy.domain.com",8080);
        mService.setWebProxy(paramWebProxy);

        ExchangeCredentials cred = new WebCredentials("[email protected]","Zuxu0000");
        NTCredentials ntCredentials = new NTCredentials("[email protected]","Zuxu0000",                 "",
                "");
        mService.setCredentials(cred);
//          ProxyHost httpProxy=new ProxyHost("ptbproxy.domain.com",8080);
//          UsernamePasswordCredentials cred1=new     UsernamePasswordCredentials("[email protected]","Zuxu0000");

//          ExchGetItemRequest req = new ExchGetItemRequest(String.valueOf(new URI("http://outlook.office365.com/EWS/Exhanges.asmx")),ntCredentials);
        ExchGetItemRequest req = new ExchGetItemRequest("https://outlook.office365.com/EWS/Exhange.asmx",ntCredentials);

        SearchFilter filter = getSearchFilter();

        PropertySet propertySet = new PropertySet(BasePropertySet.IdOnly);
        ItemView itemView = new ItemView(1000, 0, OffsetBasePoint.Beginning);
        itemView.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending);
        itemView.setPropertySet(propertySet);

        //mService.setTimeout(500000);

        req.setProperties(new    ExchMessageProperties().getExchMessageProperties(propertiesToFetch));

              /*Fetching of  mail ids start here*/
                resultMails = getMails(filter, itemView);

                return req;
    }   

    protected static SearchFilter getSearchFilter() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
    Date d1 = sdf.parse("2014-11-04 00:00:00");
    Date d2 = sdf.parse("2014-11-05 00:00:00");


        SearchFilter greaterThanEq = new SearchFilter.IsGreaterThanOrEqualTo(
                ItemSchema.DateTimeReceived,sdf.format(d1));
        SearchFilter lessThan = new SearchFilter.IsLessThan(
                ItemSchema.DateTimeReceived, sdf.format(d2));
        SearchFilter mailFilter = new SearchFilter.IsEqualTo(
                ItemSchema.ItemClass, "IPM.Note");
      return new SearchFilter.SearchFilterCollection(LogicalOperator.And,
                greaterThanEq, lessThan, mailFilter);
    }
    private static FindItemsResults<Item> getMails(SearchFilter searchFilter,
            ItemView itemView) throws Exception {
        FindItemsResults<Item> items = null;

        int count = 0;

        int maxTries = 3;

        while (true) {
            try {
                items = mService.findItems(WellKnownFolderName.Inbox,
                        searchFilter, itemView);
                break;
            } catch (Exception e) {
                if (++count == maxTries)
                    throw e;
            }
        }

        return items;
    }

}

Upvotes: 0

Eric Leschinski
Eric Leschinski

Reputation: 153872

I got the above mentioned error and spend many hours trying to fix it. My ultimate solution was to give up on EWS Version 1.2 completely and use the following javax.mail code like this:

package javaapplication4;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;


public class JavaApplication4 {
    public static void main(String[] args) throws Exception {
           new JavaApplication4().send_email();
    }
    private void send_email() throws Exception
    {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.yourserver.net");
        props.put("mail.from", "[email protected]");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.ssl.enable", "false");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");

        Authenticator authenticator = new Authenticator();
        props.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());

        Session session = Session.getInstance(props, authenticator);
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom();
        msg.setRecipients(Message.RecipientType.TO, "[email protected]");  
            // also tried @gmail.com
        msg.setSubject("JavaMail ssl test");
        msg.setSentDate(new Date());
        msg.setText("Hello, world!\n");

        Transport transport;
        transport = session.getTransport("smtp");
        transport.connect();
        msg.saveChanges(); 
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
    }
    private class Authenticator extends javax.mail.Authenticator {
       private PasswordAuthentication authentication;
       public Authenticator() {
           String username = "[email protected]";
           String password = "yourpassword";
           authentication = new PasswordAuthentication(username, password);
       }
       protected PasswordAuthentication getPasswordAuthentication() {
           return authentication;
       }
   }
}

You will need to get the javamail-1.4.7 and load the mail.jar from (http://www.oracle.com/technetwork/java/index-138643.html) into the project.

One thing we did which shed light on the situation is download Thunderbird mail client which can auto-discover information about the exchange server to make sure all of our settings were right.

Upvotes: 0

Romain Linsolas
Romain Linsolas

Reputation: 81617

The problem is due to the authentication scheme used. By default, EWS uses NTLM, but my Exchange server is not configured to accept this kind of authentication. I have to use LDAP authentication.

Upvotes: 1

Related Questions