Java:无法使用EWS API连接到corp邮箱

时间:2018-06-07 06:28:40

标签: java outlook exchange-server exchangewebservices office365api

我试图编写一个独立的java程序来访问outlook邮箱的INBOX文件夹,这也是我的公司邮箱my_username@mycompanydomain.com

我正在使用JAVA EWS API但是当我在浏览器上提示时输入usernamepassword时,它没有授权。

我正在使用的代码是:

public class EWSJavaAPI {

public static class RedirectionUrlCallback implements IAutodiscoverRedirectionUrl {
    public boolean autodiscoverRedirectionUrlValidationCallback(String redirectionUrl) {
        return redirectionUrl.toLowerCase().startsWith("https://");
    }
}

public static ExchangeService connectViaExchangeAutodiscover(String email, String password) {
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
    try {

        service.setCredentials(new WebCredentials(email, password));
        service.autodiscoverUrl(email, new RedirectionUrlCallback());
        service.setTraceEnabled(true);
        Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
        System.out.println("messages: " + inbox.getTotalCount());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return service;
}

public static void main(String[] args) {
    try {
        System.out.println("Hello World");
        ExchangeService service = connectViaExchangeAutodiscover("my_username@mycompanydomain.com", "my outlook password");

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

正如您在此处看到的,我手动加载了网址https://outlook.office365.com/EWS/Exchange.asmx,并在代码中输入了相同的凭据,但未获得授权并发出404错误。

当我尝试在service.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx"));

等代码中手动添加网址时

我得到null pointer

microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. null

我错过了什么吗?这有什么不对?确切地说,我指的是post

0 个答案:

没有答案