通过EWS连接到Outlook收件箱时出错

时间:2017-07-26 21:48:08

标签: java outlook

在发布之前,我已经通过类似的帖子,但找不到解决方案。我正在尝试使用EWS API读取我的Outlook收件箱,但我一直收到401访问被拒绝错误。请帮帮我。

我根据API需要设置了用户名,密码和域名,但不知道为什么会显示401.

public final class ExchangeServerConnection {

    private static String exchangemailurl;

    private static String userName;

    //private static String password;
    private ExchangeServerConnection() {

    }


    public static ExchangeService connectionEWS(String string) {
        // TODO Auto-generated method stub
        {
            ExchangeService service = null;
            try {
                service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
                ExchangeCredentials credentials = new WebCredentials("username@comp.com", "Uddsa#2k16", null);
                service.setCredentials(credentials);
                service.setUrl(new URI("https://email-domain.com/ews/Exchange.asmx"));
            } catch (URISyntaxException e) {

            }
            return service;
        }
    }
}

主要课程:

public class EmailUtil {
    public static void main (String args[]) throws Exception{

        ExchangeService service = ExchangeServerConnection.connectionEWS("Uaua#2k16");
        System.err.println("Connections has been set up");
        FolderView view1 = new FolderView(EWSConstants.TEN.getNumber());
        System.err.println("1");
        //  ExtendedPropertyDefinition isHiddenProp = new ExtendedPropertyDefinition(EWSConstants.PRODDEF.getNumber(),
        //  MapiPropertyType.Boolean);
        System.err.println("2");
        //PropertySet pr1 = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName, isHiddenProp);
        // view1.setPropertySet(pr1);
        System.err.println("3");
        view1.setTraversal(FolderTraversal.Deep);
        System.err.println("4");
        SearchFilter searchFilter1 = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, EWSConstants.INBOX.getValue());
        System.err.println("5");

        searchFilter1 = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilter1);
        System.err.println("6");
        FindFoldersResults findFolderResults = service.findFolders(WellKnownFolderName.MsgFolderRoot, searchFilter1, view1);
        System.err.println("7");
        Folder folder = findFolderResults.getFolders().get(0);
        System.err.println("8");
        folder.load();
        System.err.println(folder.getTotalCount());
        int messageCount = folder.getTotalCount();
        System.err.println("message count is "+messageCount);

    }
}

1 个答案:

答案 0 :(得分:0)

凭证是否正确形成?看起来您正在尝试使用电子邮件地址而不是用户/域对进行连接。是否有助于改变

ExchangeCredentials credentials = 
    new WebCredentials("username@comp.com", "*pass*", null);

ExchangeCredentials credentials = 
    new WebCredentials("username", "*pass*", "comp.com");

ExchangeCredentials credentials = 
    new WebCredentials("username", "*pass*", "*name.of.domain.the.exchange.server.is.on*");