Java SpringBoot读取Microsoft Exchange收件箱遇连接及认证问题
SpringBoot读取Microsoft Exchange收件箱报错排查过程
初始问题
使用Java SpringBoot读取Microsoft Exchange账户收件箱(已实现邮件发送功能)时,出现连接错误:
javax.mail.MessagingException: Connection dropped by server?
确认用户名和密码无误,初始配置代码如下:
Properties mailProps = new Properties(); mailProps.setProperty("mail.transport.protocol","smtp"); mailProps.setProperty("mail.smtp.auth","true"); mailProps.setProperty("mail.smtp.starttls.enable","true"); mailProps.setProperty("mail.debug","false"); mailProps.setProperty("mail.smtp.sasl.mechanisms.oauth2.oauthToken", password); Session session = Session.getDefaultInstance(mailProps); Store store = session.getStore("imaps"); store.connect("outlook.office365.com", 143, username, password); // 用户名和密码已省略 Folder emailFolder = store.getFolder("inbox"); emailFolder.open(Folder.READ_ONLY);
更新1:参照Thunderbird配置修改后
Thunderbird客户端可正常读取该收件箱,参照其配置修改代码后,出现SSL相关错误:
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle] DEBUG IMAPS: mail.imap.fetchsize: 16384 DEBUG IMAPS: mail.imap.ignorebodystructuresize: false DEBUG IMAPS: mail.imap.statuscachetimeout: 1000 DEBUG IMAPS: mail.imap.appendbuffersize: -1 DEBUG IMAPS: mail.imap.minidletime: 10 DEBUG IMAPS: enable STARTTLS DEBUG IMAPS: closeFoldersOnStoreFailure DEBUG IMAPS: trying to connect to host "outlook.office365.com", port 143, isSSL true javax.mail.MessagingException: Unsupported or unrecognized SSL message at it.spring.platform.services.communication.mail.MailTest.read(MailTest.java:53)
完整Spring收件箱读取代码:
import javax.mail.MessagingException; import javax.mail.Store; public class JavaMailReader { private Store store; private String username; private String password; private String host; private int port; private String inbox; public JavaMailReader(Store store, String host, int port, String username, String password, String inbox) { this.host=host; this.port=port; this.store=store; this.username=username; this.password=password; this.inbox=inbox; this.port = port; } public void connect() throws MessagingException { store.connect(host, port, username, password); } public Store getStore() { return store; } public String getInboxFolderName() { return this.inbox; } } @Bean public JavaMailReader emailReader(@Value("imaps") String protocol, @Value("${mailreceiver.mail.host}") String host, @Value("${mailreceiver.mail.port}") Integer port, @Value("${mailreceiver.mail.password}") String password, @Value("${mailreceiver.mail.username}") String username) throws NoSuchProviderException, MessagingException { Properties mailProps = new Properties(); mailProps.setProperty("mail.transport.protocol","imaps"); mailProps.setProperty("mail.imaps.auth","true"); mailProps.setProperty("mail.debug","true"); mailProps.setProperty("mail.imaps.sasl.mechanisms.oauth2.oauthToken", password); Session session = Session.getDefaultInstance(mailProps); Store store = session.getStore(protocol); return new JavaMailReader (store, host, port, username, password, "inbox"); } // 测试代码 @Test public void read() throws NoSuchProviderException, MessagingException { Message[] messages = mailService.getInbox(); // 对应getInbox("Inbox") Message found=null; for(Message m: messages) { if(m.getSubject().equalsIgnoreCase(subject)) { found=m; break; } } assertNotNull(found); mailService.closeReader(); }
更新2:修改端口为993并移除STARTTLS配置后
按照建议将端口改为993,移除STARTTLS配置,代码如下:
Properties mailProps = new Properties(); mailProps.setProperty("mail.transport.protocol","imaps"); mailProps.setProperty("mail.imaps.auth","true"); mailProps.setProperty("mail.debug","true"); mailProps.setProperty("mail.imaps.sasl.mechanisms.oauth2.oauthToken", password); Session session = Session.getDefaultInstance(mailProps); Store store = session.getStore(protocol); return new JavaMailReader (store, host, port, username, password, "inbox");
现在出现认证失败错误:
DEBUG IMAPS: trying to connect to host "outlook.office365.com", port 993, isSSL true * OK The Microsoft Exchange IMAP4 service is ready. [ 安全原因已省略字符串==] A0 CAPABILITY * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+ A0 OK CAPABILITY completed. DEBUG IMAPS: AUTH: PLAIN DEBUG IMAPS: AUTH: XOAUTH2 DEBUG IMAPS: protocolConnect login, host=outlook.office365.com, user= 邮箱地址已省略, password=<non-null> DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 NO AUTHENTICATE failed. Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.159 s javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
内容的提问来源于stack exchange,提问作者Discipulos
相关产品推荐
相关产品推荐

