AuthenticationFailedException:AUTHENTICATE 在某些连接后失败

AuthenticationFailedException: AUTHENTICATE failed After some connections

我有一个 Spring-Boot (1.4.0) 应用程序,我正在使用 springframework.boot:spring-boot-starter-mail

我有一个用 @Scheduled 注释的方法每隔一段时间检查我的收件箱。

这是我获取收件箱的方式:

private static Folder getInbox() throws MessagingException {
final String protocol = "mail.store.protocol";
        final String storeType = "imaps";
        final String email = "email";
        final String password = "password";
        final String connect = "webmail.company.com";
        final String folder = "INBOX";

        final Properties props = new Properties();
        props.setProperty(protocol, storeType);

        final Session session = Session.getInstance(props, null);
        final Store store = session.getStore();
        store.connect(connect, email, password);

        final Folder inbox = store.getFolder(folder);
        inbox.open(Folder.READ_WRITE);
        return inbox;
    }

然后我有这个:

@Scheduled(fixedRate = 10000)
    @Override
    public void checkEmailCreateCompanyAndSendCsv() throws MessagingException, IOException {
    log.info("Checking e-mail...");
            final Folder inbox = getInbox();

            final Flags seen = new Flags(Flags.Flag.SEEN);
            final FlagTerm unseenFlagTerm = new FlagTerm(seen, false);

            inbox.getMessages();
            final Message messages[] = inbox.search(unseenFlagTerm);


.....
.....
}

当应用程序 运行 运行时一切正常,但一段时间后(大约 7 到 8 次电子邮件检查)它开始抛出异常:

javax.mail.AuthenticationFailedException: AUTHENTICATE failed. at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:717) at javax.mail.Service.connect(Service.java:366) at javax.mail.Service.connect(Service.java:246) at com.opessoftware.crs.selfcertification.services.EmailServiceBasic.getInbox(EmailServiceBasic.java:183) at com.opessoftware.crs.selfcertification.services.EmailServiceBasic.checkEmailCreateCompanyAndSendCsv(EmailServiceBasic.java:50) at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

如果我停止应用程序并再次 运行 它,错误就会消失并且 cicle 会再次启动。

有什么建议吗?

我刚刚解决了。

我调用连接的次数太多,超出了电子邮件服务器的连接池。所以我所做的是将 getInbox()getSession() 定义为 @BeanInject 我的 EmailService

现在它只创建一次连接。它工作正常。