使用javax邮件访问需要SSO登录的Outlook电子邮件时出错

时间:2018-03-05 20:29:57

标签: outlook javamail imap pop3

我正在尝试使用javax邮件阅读组织内的Outlook电子邮件,但我一直收到错误 - “javax.mail.AuthenticationFailedException:登录失败:未知用户名或密码错误。”

需要注意的一点是登录Outlook时需要单点登录。因此,您必须输入两次相同的凭据(emailID /密码)。

以下是我的代码 -

public static void main(String[] args) {
    String host = "pop.outlook.com";
    String mailStoreType = "pop3";
    String username = "firstname.lastname@companyName.com";
    String password = "password";
    check(host, mailStoreType, username, password);
}

public static void check(String host, String mailStoreType, String user, String password)
{
    try {
        //create properties field
        Properties props = new Properties();
        props.setProperty("mail.pop3.ssl.enable", "true");
        props.setProperty("mail.pop3.auth.plain.disable", "true");
        props.put("mail.pop3.host", host);
        props.put("mail.pop3.port", "995");
        props.setProperty("mail.pop3.starttls.enable", "true");
        Session emailSession = Session.getDefaultInstance(props);

        //create the POP3 store object and connect with the pop server
        Store store = emailSession.getStore(mailStoreType);
        store.connect(host, user, password);

        //create the folder object and open it
        Folder emailFolder = store.getFolder("INBOX");
        emailFolder.open(Folder.READ_ONLY);

        // retrieve the messages from the folder in an array and print it
        Message[] messages = emailFolder.getMessages();
        Logger.logConsoleMessage("messages.length---" + messages.length);

        for (int i = 0, n = messages.length; i < n; i++) {
            Message message = messages[i];
            Logger.logConsoleMessage("---------------------------------");
            Logger.logConsoleMessage("Email Number " + (i + 1));
            Logger.logConsoleMessage("Subject: " + message.getSubject());
            Logger.logConsoleMessage("From: " + message.getFrom()[0]);
            Logger.logConsoleMessage("Text: " + message.getContent().toString());
        }
        //close the store and folder objects
        emailFolder.close(false);
        store.close();

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

稍微调整上面的代码后,我可以访问一个Gmail帐户,这让我相信上面的错误是因为SSO需要额外的身份验证。有没有人遇到过这个?非常感谢任何投入,谢谢!

0 个答案:

没有答案