无法建立与Exchange Web服务的连接 - Java API [英] Could not establish the connection to the Exchange Web Service - Java API

查看:748
本文介绍了无法建立与Exchange Web服务的连接 - Java API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试 Microsoft Exchange Web服务Java API (版本1.2)以从服务器读取邮件。这是我的代码:

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("foo@bar.com");
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?

请注意网络服务( https // my-server / exchange我的Java代码可以访问.asmx

不确定它是否有帮助,但我发现在显示的跟踪中日志:

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>






编辑

我做了另一个测试。这一次,我使用默认凭据 - 所以我自己的电子邮件帐户 - 使用 service.setUseDefaultCredentials(true); 而不是设置 WebCredentials 对象。连接仍然没有建立,但我得到另一个错误(我只采用了跟踪日志的有趣部分):

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>

当然,我无法在Exchange服务器端访问或更改任何内容。有没有办法使身份验证成功?

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

推荐答案

问题是由于使用了身份验证方案。默认情况下,EWS使用NTLM,但我的Exchange服务器未配置为接受此类身份验证。我必须使用LDAP身份验证。

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.

这篇关于无法建立与Exchange Web服务的连接 - Java API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆