"Mailbox does not exist." 在 EWS 上获取电子邮件时

"Mailbox does not exist." when fetching emails on EWS

我正在尝试使用 Exchange Web 服务 (EWS) 访问 Office365 邮箱上的电子邮件。

我的 O365 管理员已创建:

我可以使用 appId/tenantId 和 UserNamePasswordParameters 使用帐户凭据检索 Oauth 令牌,现在我正尝试从邮箱检索电子邮件,但出现此错误:

microsoft.exchange.webservices.data.core.exception.service.remote.ServiceResponseException: Mailbox does not exist.

这是我的代码:

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

现在可以正常使用了,我可以收到电子邮件了。