I am using the following Java code in my Spring Boot App (v2.1.6.RELEASE):
@Bean
public IntegrationFlow mailListener() {
SearchTermStrategy searchTermStrategy = (supportedFlags, folder) -> {
SearchTerm search = new AndTerm(new SubjectTerm("trigger the build"), new FlagTerm(new Flags(Flags.Flag.SEEN), false));
return search;
};
return IntegrationFlows.from(Mail.imapInboundAdapter("imaps://usr:pwd@imap.host.com/INBOX").searchTermStrategy(searchTermStrategy).shouldMarkMessagesAsRead(true).get(),
e -> e.poller(Pollers.fixedRate(5000).maxMessagesPerPoll(1)))
.<Message>handle((payload, header) -> logMail(payload))
.get();
}
But this throws an exception com.sun.mail.iap.CommandFailedException: A7 NO Command received in Invalid state.
when there is a matching message. This seems to be an issue with the IMAP folder being set as Read-only by default.
There is an onInit
protected method in the AbstractMailReceiver
class which opens the Folder in Read-Write mode. And I have tried to call the ImapMailReceiver.afterPropertiesSet()
which eventually calls the onInit
method but that gives me An external 'receiver' ... can't be modified.
:
@Bean
private ImapMailReceiver receiver() {
ImapMailReceiver receiver = new ImapMailReceiver("...URL...");
receiver.afterPropertiesSet();
//receiver.setJavaMailProperties(javaMailProperties);
return receiver;
}
Can someone please guide me on how to set the folder in read-write mode?
P.S. In plain Java code I could set the same using inbox.open(Folder.READ_WRITE)
and this works.
If I use the code like this:
@Bean
public IntegrationFlow mailListener() {
SearchTermStrategy searchTermStrategy = (supportedFlags, folder) -> {
SearchTerm search = new AndTerm(new SubjectTerm("trigger the build"),
new FlagTerm(new Flags(Flags.Flag.SEEN), false));
return search;
};
return IntegrationFlows.from(Mail.imapInboundAdapter(receiver()))
.log()
.get();
}
@Bean
public ImapMailReceiver receiver() {
return new ImapMailReceiver(IMAP_URL);
}
I get the following error:
Caused by: java.lang.IllegalArgumentException: No poller has been defined for channel-adapter 'mailListener.org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean#0', and no default poller is available within the context.
at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean.initializeAdapter(SourcePollingChannelAdapterFactoryBean.java:186) ~[spring-integration-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean.afterPropertiesSet(SourcePollingChannelAdapterFactoryBean.java:144) ~[spring-integration-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
... 26 common frames omitted
If I use the code as below:
@Bean
public IntegrationFlow mailListener() {
SearchTermStrategy searchTermStrategy = (supportedFlags, folder) -> {
SearchTerm search = new AndTerm(new SubjectTerm("trigger the build"),
new FlagTerm(new Flags(Flags.Flag.SEEN), false));
return search;
};
return IntegrationFlows.from(Mail.imapInboundAdapter(receiver()).searchTermStrategy(searchTermStrategy).shouldMarkMessagesAsRead(true).get(),
e -> e.poller(Pollers.fixedRate(5000).maxMessagesPerPoll(1)))
.<Message>handle((payload, header) -> logMail(payload))
.get();
}
@Bean
public ImapMailReceiver receiver() {
return new ImapMailReceiver(IMAP_URL);
}
I get the following error:
Caused by: java.lang.IllegalStateException: An external 'receiver' [imaps://.../INBOX] can't be modified.
at org.springframework.util.Assert.state(Assert.java:94) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.integration.mail.dsl.MailInboundChannelAdapterSpec.assertReceiver(MailInboundChannelAdapterSpec.java:85) ~[spring-integration-mail-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.integration.mail.dsl.ImapMailInboundChannelAdapterSpec.searchTermStrategy(ImapMailInboundChannelAdapterSpec.java:51) ~[spring-integration-mail-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at com.xxx.emailapp.InboundEmailConfig.mailListener(InboundEmailConfig.java:33) ~[classes/:na]
at com.xxx.emailapp.InboundEmailConfig$$EnhancerBySpringCGLIB$$7ab01b59.CGLIB$mailListener$1(<generated>) ~[classes/:na]
at com.xxx.emailapp.InboundEmailConfig$$EnhancerBySpringCGLIB$$7ab01b59$$FastClassBySpringCGLIB$$8ee22ea2.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at com.xxx.emailapp.InboundEmailConfig$$EnhancerBySpringCGLIB$$7ab01b59.mailListener(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
... 19 common frames omitted
Declaring an explicit receiver bean works
@Bean
public IntegrationFlow mailListener() {
SearchTermStrategy searchTermStrategy = (supportedFlags, folder) -> {
SearchTerm search = new AndTerm(new SubjectTerm("trigger the build"),
new FlagTerm(new Flags(Flags.Flag.SEEN), false));
return search;
};
return IntegrationFlows.from(Mail.imapInboundAdapter(receiver()))
.log()
.get();
}
@Bean
public ImapMailReceiver receiver() {
return new ImapMailReceiver("imaps://usr:pwd@imap.host.com/INBOX");
}
EDIT
You must not call get()
on the receiver spec; the framework needs a reference to the spec itself to properly initialize the bean(s).