我正在尝试使用Exchange Web Services (EWS)访问Office365邮箱上的电子邮件。
我的O365管理员创建了:
- 共享邮箱:shared@domain.com
- 账号:my.account@domain.com
- 一个组,允许访问邮箱 上的帐户
我能够使用appId/tenantId和使用帐户凭据的UserNamePasswordParameters
检索Oauth令牌,现在我试图从邮箱检索电子邮件,但我得到此错误:
microsoft.exchange.webservices.data.core.exception.service.remote.ServiceResponseException:邮箱不存在
这是我的代码:
public Iterable fetchEmails(String token, String account) throws Exception {
if(token==null) {
token = getToken();
}
FindItemsResults<Item> emails;
try (ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2)) {
service.getHttpHeaders().put("Authorization", "Bearer " + token);
service.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx"));
service.setWebProxy(new WebProxy(PROXY_HOST, PROXY_PORT));
FolderId folder = new FolderId(WellKnownFolderName.Inbox, new Mailbox(account));
emails = service.findItems(folder, new ItemView(15));
}
return emails;
}
好吧,这是有点愚蠢…我很困惑,因为这个帐户也是一个电子邮件。
解决方案是在构建Mailbox
对象时传递邮箱(shared@domain.com)而不是帐户(my.account@domain.com):
FolderId folder = new FolderId(WellKnownFolderName.Inbox, new Mailbox("shared@domain.com"));
现在它工作了,我可以收到邮件了。