2

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.

Mahmoud
  • 29
  • 1
  • 5

3 Answers3

2

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.

user1017413
  • 2,023
  • 4
  • 26
  • 41
  • how would i know the specific class & block code that is throwing the error. – Mahmoud Jul 16 '14 at 07:31
  • Those two methods in SimpleServiceRequestBase are shared for most service calls. Walk through them and check the lines from the exception. Alternatively, you can use a TraceListener attached to the ExchangeService to dump the SOAP request/response to the console (or wherever you'd like) and see if it's an outright error in the response or if it might be choking on a piece of XML parsing. – user1017413 Jul 21 '14 at 16:50
2

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.

P44T
  • 1,109
  • 12
  • 21
0

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.