Mahmoud
Mahmoud

Reputation: 21

microsoft.exchange.webservices.data.EWSHttpException: Connection not established

i am trying to use EWSJavaAPI 1.2 to send email from exchange server 2007 email as follows:

        String email="myuser@mydomain";
        String password="mypass";
        String host="myhost";
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        ExchangeCredentials credentials = new WebCredentials(email, password);
        service.setCredentials(credentials);
        service.setUrl(new java.net.URI("https://" + host
            + "/EWS/Exchange.asmx"));
        service.setTraceEnabled(true);
        service.setTimeout(60 * 1000);


        EmailMessage msg = new EmailMessage(service);
        msg.setSubject("Hello world!");
        msg.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS Managed API."));
        msg.getToRecipients().add("user2@mydomain");
        msg.send();

but i am getting the following error:

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.internalCreateItems(Unknown Source)
    at microsoft.exchange.webservices.data.ExchangeService.createItem(Unknown Source)
    at microsoft.exchange.webservices.data.Item.internalCreate(Unknown Source)
    at microsoft.exchange.webservices.data.EmailMessage.internalSend(Unknown Source)
    at microsoft.exchange.webservices.data.EmailMessage.send(Unknown Source)

and the trace:

<Trace Tag="EwsRequestHttpHeaders" Tid="1" Time="2014-07-15 11:44:43Z">
POST /EWS/Exchange.asmx HTTP/1.1
Content-type : text/xml; charset=utf-8
Accept-Encoding : gzip,deflate
Keep-Alive : 300
User-Agent : ExchangeServicesClient/0.0.0.0
Connection : Keep-Alive
Accept : text/xml


</Trace>

<Trace Tag="EwsRequest" Tid="1" Time="2014-07-15 11:44:43Z">
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"><soap:Header><t:RequestServerVersion Version="Exchange2007"></t:RequestServerVersion></soap:Header><soap:Body><m:CreateItem MessageDisposition="SendOnly"><m:Items><t:Message><t:Subject>Hello world!</t:Subject><t:Body BodyType="HTML">Sent using the EWS Managed API.</t:Body><t:ToRecipients><t:Mailbox><t:EmailAddress>myuser@mydomain</t:EmailAddress></t:Mailbox></t:ToRecipients></t:Message></m:Items></m:CreateItem></soap:Body></soap:Envelope>
</Trace>

<Trace Tag="EwsResponseHttpHeaders" Tid="1" Time="2014-07-15 11:44:43Z">
401 null
WWW-Authenticate : Basic realm="myhost"
Date : Tue, 15 Jul 2014 11:44:43 GMT
Content-Length : 0
X-Powered-By : ASP.NET
Server : Microsoft-IIS/7.0


</Trace>

<Trace Tag="EwsResponseHttpHeaders" Tid="1" Time="2014-07-15 11:44:43Z">
401 null
WWW-Authenticate : Basic realm="myhost"
Date : Tue, 15 Jul 2014 11:44:43 GMT
Content-Length : 0
X-Powered-By : ASP.NET
Server : Microsoft-IIS/7.0


</Trace>

<Trace Tag="EwsResponse" Tid="1" Time="2014-07-15 11:44:43Z">
Non-textual response
</Trace>

BTW, when i tried to access the url:

https://myhost/EWS/Exchange.asmx

it redirects me to the following url:

https://myhost/EWS/Services.wsdl

please advise how to fix this issue.

Upvotes: 2

Views: 3688

Answers (3)

user3247321
user3247321

Reputation:

I have faced the same issue.Check your UserName and Password, It may be wrong. According to the trace "401" Status code signifies that you are Unauthorized user and u might be getting connection not Connection not established as an error message.

Upvotes: 0

P44T
P44T

Reputation: 1179

I think this is a bug in the EWS Java API library, which i fixed recently (see this pull request). You should try to use the updated library from the official ews-java-api repository (for now you'll have to build it yourself) and see if it works now.

Upvotes: 2

user1017413
user1017413

Reputation: 2183

That's a bug in EWS Java. If there's an issue reading the response in SimpleServiceRequestBase.readResponse(), it will close the response in a finally block, then kick it back to the calling method (in this case, internalExecute()), which will try to read the headers on the response. Since the response was closed, this will throw a NullPointerException, which gets wrapped in a ServiceRequestException, effectively hiding the original Exception and preventing you from seeing it in the stack trace.

Fix the bug and you'll probably have an easier time reading the actual error. Get used to bugs in EWS Java. I've found over 30 of them and counting.

Upvotes: 2

Related Questions