I'm using spring boot 2.2.4.RELEASE
I need to build a dynamic mail receiver because I can have several mail server where to fetch mails. The mail server must be configurable by other systems so my requirement is to be dynamically able in fetching messages.
I investigated and I liked the Spring Integration solution and its DSL (note: it's enough to me to simply download messages and their attachments, if any).
So I built this code:
String flowId = MAIL_IN_FLOW_ID_PREFIX+cpd.getIndirizzoMail();
if( flowContext.getRegistrationById(flowId) != null ) {
flowContext.remove(flowId);
}
ImapMailInboundChannelAdapterSpec adapterSpec = Mail.imapInboundAdapter(connectionUrl.toString())
.javaMailProperties(javaMailProperties)
.shouldDeleteMessages(false)
.shouldMarkMessagesAsRead(false)
.selector(selectFunction);
if( confMailIn.isRichiedeAutenticazione() ) {
adapterSpec = adapterSpec.javaMailAuthenticator(new CasellaPostaleAuthenticator(cpd.getUsername(), cpd.getPassword()));
}
IntegrationFlow flow = IntegrationFlows
.from(adapterSpec.get(), e -> e.poller(Pollers.fixedDelay(Duration.ofSeconds(pollingSeconds)).maxMessagesPerPoll(maxMailMessagePerPoll)))
.handle(message -> {
logger.info("Message headers "+message.getHeaders());
logger.info("Message payload "+message.getPayload());
})
.get();
flowContext.registration(flow).id(flowId).register();
I tried by using my gmail account. The code is able in connecting to GMAIL by imap but, when I try to simply log messages, I get this error:
A6 OK Success
2020-02-05 12:48:41,835 23412 [task-scheduler-1] DEBUG o.s.i.mail.ImapMailReceiver - Received 10 messages
2020-02-05 12:48:41,836 23413 [task-scheduler-1] DEBUG oA7 STORE 1 +FLAGS (\Flagged) .s.i.mail.ImapMailReceiver - USER flags are not supported by this mail server. Flagging message with system flag
A7 NO STORE attempt on READ-ONLY folder (Failure)
A8 CLOSE
A8 OK Returned to authenticated state. (Success)
DEBUG IMAP: added an Authenticated connection -- size: 1
2020-02-05 12:48:42,198 23775 [task-scheduler-1] ERROR o.s.i.handler.LoggingHandler - org.springframework.messaging.MessagingException: failure occurred while polling for mail; nested exception is javax.mail.MessagingException: A7 NO STORE attempt on READ-ONLY folder (Failure); nested exception is: com.sun.mail.iap.CommandFailedException: A7 NO STORE attempt on READ-ONLY folder (Failure) at org.springframework.integration.mail.MailReceivingMessageSource.doReceive(MailReceivingMessageSource.java:74) at org.springframework.integration.endpoint.AbstractMessageSource.receive(AbstractMessageSource.java:167) at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:250)
Now it seems that by default the FOLDER is opened in READ_ONLY way and this seems to cause an error.
I'm stucked here and I can't figure out how to solve the issue.
May anybody give me a tip?
Thank you
Angelo
adapterSpec.get()
Issuing a get()
on the spec circumvents Spring's bean initialization logic which switches the folder to read/write.
Either make the adapter a @Bean
or simply remove the .get()
and Spring will perform the initialization.
.from(adapterSpec, e -> ...