0
votes

I have a Spring-Boot (1.4.0) application and I am using springframework.boot:spring-boot-starter-mail.

I have a method annotated with @Scheduled to check my inbox every a certain period of time.

This is how I get my inbox:

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;
    }

Then I have this:

@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);


.....
.....
}

When the APP is running everything works great but after some time (around 7 to 8 e-mail checks) it starts to thrown an exception:

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.access$301(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)

If I stop the application and run it again the error disappears and the cicle starts again.

Any suggestions?

1

1 Answers

0
votes

I just solved it.

I was calling the connection too many times and I was exceeding the connection pool of my e-mail server. So what I did is to define the getInbox() and the getSession() as @Bean and Inject them to my EmailService.

Now it creates the connection only once. It is working fine.