1
votes

I am trying to access emails on a Office365 mailbox, using Exchange Web Services (EWS).

My O365 admin has created :

I am able to retrieve the Oauth token using the appId/tenantId and a UserNamePasswordParameters using the account's credentials, and now I am trying to retrieve the emails from the mailbox, but I get this error :

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

here's my code :

  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;
  }
1
Hi, Can I see your getToken () method. I am creating an outlook web addin using similar code, and can't decide if I should use graph API or EWS? - Asmi
EWS is deprecated and there will be no extra feature added to it. If you can, you should use graph AP - Vincent F

1 Answers

1
votes

OK, it was a bit stupid.. I got confused because the account is also an email..

solution is to pass the mailbox ([email protected]) instead of the account ([email protected]) when building the Mailbox object :

FolderId folder = new FolderId(WellKnownFolderName.Inbox, new Mailbox("[email protected]"));

And now it's working, I am able to get the emails.