Java EWS API - 读取非默认邮箱

时间:2013-08-13 08:06:13

标签: java exchangewebservices ewsjavaapi

我想在使用Java EWS API的Exchange服务器上读取非默认邮箱,但我的代码有问题。以下是相关部分的摘录:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
ExchangeCredentials credentials = new WebCredentials("<user>", "<pass>");
service.setCredentials(credentials);
service.setUrl(new URI("https://<URL>/EWS/Exchange.asmx"));
ItemView iview = new ItemView(3);
Mailbox mb = new Mailbox();
mb.setAddress("<mailbox_address>");
FolderId folderId = new FolderId(WellKnownFolderName.Root, mb);
FindItemsResults<Item> findResults = service.findItems(folderId, iview);

错误信息是:

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.getResponseHeaders(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)

顺便说一下。我能够读取我的默认邮箱,发送电子邮件等...

你能告诉我吗?提前谢谢!

2 个答案:

答案 0 :(得分:0)

我相信您正在尝试访问一个邮箱,但使用其他帐户的凭据。这可以通过模仿来解决。

在较新版本的EWS中,可以解决in AD on the exchange serverby programmatically setting the ConnectingSID property。我可能会在第二个上进行存储,并使主SMTP地址可以针对不同的环境进行配置。

以下是在C#中使用EWS 2010的代码示例,因此您可能必须使用setCredentials(credentials),依此类推:

代码示例EWS 2010

ExchangeServiceBinding binding = new ExchangeServiceBinding();
// Use this class instead of ExchangeService (!)
binding.Credentials = credentials;  
// etc    

binding.ExchangeImpersonation = new ExchangeImpersonationType();
binding.ExchangeImpersonation.ConnectingSID = new ConnectingSIDType();
binding.ExchangeImpersonation.ConnectingSID.PrimarySmtpAddress = "<mailbox_address>";

EWS 2007的文档

根据{{​​3}},似乎在EWS 2007 SP1中也应该可以使用它。请注意,从文章中可以看出:

  

您必须设置权限“ms-Exch-EPI-Impersonation”必须位于将进行模拟的服务帐户的客户端访问服务器上。此外,服务帐户必须在其将模拟的每个邮箱上授予“ms-Exch-EPI-May-Impersonate”权限。如果您要将其设置为商店,存储组,服务器或组织级别,则此权限将继承到邮箱。

答案 1 :(得分:0)

此代码似乎是正确的,并且如果该代码适用于您的默认邮箱,则可能是某些权限错误。

我正在使用类似的代码来访问共享邮件组的邮件

我看到的唯一区别是-> WellKnownFolderName.MsgFolderRoot 而不是 WellKnownFolderName.Root