ImapMailReceiver NO STORE attempt on READ-ONLY fol

2020-04-16 18:12发布

问题:

I spend day trying to find some explanation this exception. I try to configure ImapMailReceiver and ImapIdleChannelAdapter programmatic.

I didn't find any tutorials about this question just small info in Pro Spring Integration book.

public void loadMessages() {
        ImapIdleChannelAdapter imapIdleChannelAdapter = null;
        ImapMailReceiver imapMailReceiver = null;
        try {
            imapMailReceiver = new ImapMailReceiver("imaps://" + URLEncoder.encode(USERNAME, "UTF-8") + ":" + PASSWORD + "@imap.gmail.com:993/INBOX");
            imapMailReceiver.setShouldMarkMessagesAsRead(true);
            imapMailReceiver.setShouldDeleteMessages(false);

            Properties javaMailProperties = new Properties();
            javaMailProperties.put(MAIL_IMAP_SOCKET_FACTORY_CLASS, environment.getProperty(MAIL_IMAP_SOCKET_FACTORY_CLASS, SOCKET_FACTORY_CLASS));
            javaMailProperties.put(MAIL_IMAP_SOCKET_FACTORY_FALLBACK, environment.getProperty(MAIL_IMAP_SOCKET_FACTORY_FALLBACK, Boolean.class, SOCKET_FACTORY_FALLBACK));
            javaMailProperties.put(MAIL_STORE_PROTOCOL, environment.getProperty(MAIL_STORE_PROTOCOL, INBOX_MAIL_PROTOCOL));
            javaMailProperties.put(MAIL_DEBUG, environment.getProperty(MAIL_DEBUG, Boolean.class, MAIL_DEBUG_VAL));
            imapMailReceiver.setJavaMailProperties(javaMailProperties);

            imapMailReceiver.setJavaMailAuthenticator(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(USERNAME, PASSWORD);
                }
            });


            ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
            threadPoolTaskScheduler.setPoolSize(environment.getProperty(RECEIVE_MESSAGE_POOL_SIZE, Integer.class, DEFAULT_RECEIVE_MESSAGE_POOL_SIZE));
            threadPoolTaskScheduler.afterPropertiesSet();

            DirectChannel directChannel = new DirectChannel();
            directChannel.subscribe(new MessageHandler() {
                @Override
                public void handleMessage(Message<?> message) throws org.springframework.messaging.MessagingException {
                    LOGGER.info("Message: " + message);

                }
            });


            imapIdleChannelAdapter = new ImapIdleChannelAdapter(imapMailReceiver);
            imapIdleChannelAdapter.setAutoStartup(true);
            imapIdleChannelAdapter.setShouldReconnectAutomatically(true);
            imapIdleChannelAdapter.setTaskScheduler(threadPoolTaskScheduler);
            imapIdleChannelAdapter.setOutputChannel(directChannel);
            imapIdleChannelAdapter.start();
        } catch (IllegalStateException iex){
            LOGGER.debug("Receiving messages failed", iex);
            iex.printStackTrace();
            imapIdleChannelAdapter.stop();
            try{
                imapMailReceiver.destroy();
            } catch (Exception ex){
                LOGGER.debug("Impossible destroy imapMailReceiver", ex);
                ex.printStackTrace();
            }
        } catch (UnsupportedEncodingException uex) {
            LOGGER.debug("UnsupportedEncoding Exception", uex);
            uex.printStackTrace();
        }
    }

My stacktrace

09:13:27,429  INFO ImapMailReceiver:251 - attempting to receive mail from folder [INBOX]
09:13:34,840  WARN ImapIdleChannelAdapter:230 - error occurred in idle task
javax.mail.MessagingException: A13 NO STORE attempt on READ-ONLY folder (Failure) [THROTTLED];
  nested exception is:
    com.sun.mail.iap.CommandFailedException: A13 NO STORE attempt on READ-ONLY folder (Failure) [THROTTLED]
    at com.sun.mail.imap.IMAPMessage.setFlags(IMAPMessage.java:858)
    at javax.mail.Message.setFlag(Message.java:574)
    at org.springframework.integration.mail.AbstractMailReceiver.setMessageFlags(AbstractMailReceiver.java:317)
    at org.springframework.integration.mail.AbstractMailReceiver.postProcessFilteredMessages(AbstractMailReceiver.java:283)
    at org.springframework.integration.mail.AbstractMailReceiver.receive(AbstractMailReceiver.java:272)
    at org.springframework.integration.mail.ImapIdleChannelAdapter$IdleTask.run(ImapIdleChannelAdapter.java:216)
    at org.springframework.integration.mail.ImapIdleChannelAdapter$ReceivingTask.run(ImapIdleChannelAdapter.java:184)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

I also find such type of decision

http://harikrishnan83.wordpress.com/2009/01/24/access-gmail-with-imap-using-java-mail-api/ http://metoojava.wordpress.com/2010/03/21/java-code-to-receive-mail-using-javamailapi/

But I can't understand why people use this way for getting messages (where the main source this decision because I can't even find this type of solution in Spring Pro Integration and Spring documentation )

Please, can anyone explain me 1)in which case I should use ImapIdleChannelAdapter and when just Session for getting store and connecting to mailbox. 2)Why do I get the exception 'NO STORE attempt on READ-ONLY folder' now

回答1:

You should call imapMailReceiver.afterPropertiesSet(), which switches this.folderOpenMode = Folder.READ_WRITE;. In your case it looks lie here:

imapMailReceiver.setJavaMailAuthenticator(new Authenticator() {
...
});

imapMailReceiver.afterPropertiesSet();

Actually almost all Spring Integration components should be configured as Spring beans and Container will take care about their initialization. The new 4.0 version provides enough options for JavaConfig: https://spring.io/blog/2014/04/30/spring-integration-4-0-released .

It's not clear to me why by default it is Folder.READ_ONLY and there is no setter on the matter.

Feel free to raise a JIRA issue: https://jira.spring.io/browse/INT

In which case I should use ImapIdleChannelAdapter and when just Session for getting store and connecting to mailbox.

Actually it depends on your Mail provider. Spring Integration provides two components: Pop3MailReceiver and ImapMailReceiver. Of course Imap is better, because it doesn't fetch messages to the local directory and supports Listener. Pop3 you should ping periodically.