使用javaMail读取共享邮箱

时间:2017-12-07 20:15:36

标签: java email javamail

我正在创建一个能够从共享邮箱中读取电子邮件的界面。我已经尝试了几种不同的方法,我可以在堆栈中找到,但没有一个工作。例子:

Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imaps.socketFactory.fallback", "false");
props.setProperty("mail.imaps.port", "993");
props.setProperty("mail.imaps.socketFactory.port", "993");
props.put("mail.imaps.host", "outlook.office365.com");



try {
    /* Create the session and get the store for read the mail. */

    Session session = Session.getDefaultInstance(System.getProperties(), null);
    Store store = session.getStore("imaps");

    store.connect("outlook.office365.com", 993, "eduardo.david@hpe.com/APSDataExtract", "-myPassword-");

给我以下结果:

Exception while connecting to server: AUTHENTICATE failed.
javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:661)
at javax.mail.Service.connect(Service.java:295)
at t2.OutLookReader_imaps.<init>(OutLookReader_imaps.java:39)
at t2.OutLookReader_imaps.main(OutLookReader_imaps.java:121)

如果我尝试其他代码,结果是一样的:

store.connect("outlook.office365.com", 993, "hpe\\eduardo.david@hpe.com\\APSDataExtract", "-myPassword-");

结果:

Exception while connecting to server: * BYE Connection is closed. 13
javax.mail.MessagingException: * BYE Connection is closed. 13;
  nested exception is:
com.sun.mail.iap.ConnectionException: * BYE Connection is closed. 13
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:668)
at javax.mail.Service.connect(Service.java:295)
at t2.OutLookReader_imaps.<init>(OutLookReader_imaps.java:39)
at t2.OutLookReader_imaps.main(OutLookReader_imaps.java:121)
Caused by: com.sun.mail.iap.ConnectionException: * BYE Connection is closed. 13
at com.sun.mail.iap.Protocol.handleResult(Protocol.java:356)
at com.sun.mail.imap.protocol.IMAPProtocol.authplain(IMAPProtocol.java:603)
at com.sun.mail.imap.IMAPStore.login(IMAPStore.java:736)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:642)
    ... 3 more
store.connect("outlook.office365.com", 993, "hpe\\eduardo.david\\APSDataExtract", "-myPassword-");

结果:

Inside MailReader()...
Exception while connecting to server: * BYE Connection is closed. 13
javax.mail.MessagingException: * BYE Connection is closed. 13;
  nested exception is:
com.sun.mail.iap.ConnectionException: * BYE Connection is closed. 13
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:668)
at javax.mail.Service.connect(Service.java:295)
at t2.OutLookReader_imaps.<init>(OutLookReader_imaps.java:39)
at t2.OutLookReader_imaps.main(OutLookReader_imaps.java:121)
Caused by: com.sun.mail.iap.ConnectionException: * BYE Connection is closed. 13
at com.sun.mail.iap.Protocol.handleResult(Protocol.java:356)
at com.sun.mail.imap.protocol.IMAPProtocol.authplain(IMAPProtocol.java:603)
at com.sun.mail.imap.IMAPStore.login(IMAPStore.java:736)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:642)

......还有3个

store.connect("outlook.office365.com", 993, "hpe\\davidedu\\APSDataExtract", "-myPassword-");

结果:

Inside MailReader()...
Exception while connecting to server: * BYE Connection is closed. 13
javax.mail.MessagingException: * BYE Connection is closed. 13;
  nested exception is:
com.sun.mail.iap.ConnectionException: * BYE Connection is closed. 13
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:668)
at javax.mail.Service.connect(Service.java:295)
at t2.OutLookReader_imaps.<init>(OutLookReader_imaps.java:39)
at t2.OutLookReader_imaps.main(OutLookReader_imaps.java:121)
Caused by: com.sun.mail.iap.ConnectionException: * BYE Connection is closed. 13
at com.sun.mail.iap.Protocol.handleResult(Protocol.java:356)
at com.sun.mail.imap.protocol.IMAPProtocol.authplain(IMAPProtocol.java:603)
at com.sun.mail.imap.IMAPStore.login(IMAPStore.java:736)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:642)
... 3 more

要访问我自己的收件箱,代码可以正常运行:

store.connect("outlook.office365.com", 993, "eduardo.david@hpe.com", "-myPassword-");

Inside MailReader()...... 未读消息数:580 消息数量:849 已删除邮件数:2

任何人都可以给这个java新程序员一个简单的解决方案来读取共享邮箱吗?

1 个答案:

答案 0 :(得分:1)

我解决了它添加这些参数:

sysProps.setProperty("mail.imaps.auth.plain.disable", "true");
sysProps.setProperty("mail.imaps.auth.ntlm.disable", "true");

我使用:

连接
 store.connect("outlook.office365.com", "name@abc.com\\sharedmailAlias", "password");
  • 我的电子邮件:name@abc.com
  • 我的共享电子邮件地址: sharedmailAlias@abc.com