microsoft.exchange.webservices.data.EWSHttpException:未建立连接

时间:2014-07-15 11:47:42

标签: javamail exchangewebservices exchange-server-2007 ewsjavaapi

我正在尝试使用EWSJavaAPI 1.2从Exchange Server 2007电子邮件发送电子邮件,如下所示:

        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();

但是我收到以下错误:

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)

和跟踪:

<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,当我试图访问网址时:

https://myhost/EWS/Exchange.asmx

它将我重定向到以下网址:

https://myhost/EWS/Services.wsdl

请告知如何解决此问题。

3 个答案:

答案 0 :(得分:2)

这是EWS Java中的一个错误。如果在SimpleServiceRequestBase.readResponse()中读取响应时出现问题,它将关闭finally块中的响应,然后将其踢回调用方法(在本例中为internalExecute()),该方法将尝试读取头文件响应。由于响应被关闭,这将抛出一个NullPointerException,它被包装在ServiceRequestException中,有效地隐藏了原始的Exception并阻止您在堆栈跟踪中看到它。

修复错误,您可能会更容易阅读实际错误。习惯EWS Java中的错误。我发现其中有30多个并且数不胜数。

答案 1 :(得分:2)

我认为这是EWS Java API库中的一个错误,我最近修复了它(参见this pull request)。您应该尝试使用the official ews-java-api repository中的更新库(现在您必须自己构建它)并查看它是否现在正常工作。

答案 2 :(得分:0)

我遇到了同样的问题。检查你的用户名和密码,这可能是错的。 根据跟踪“401”状态代码表示您是未经授权的用户,并且您可能正在获取连接而未将连接未建立为错误消息。