如何在 Spring Mail Integration 中启用 IMAPFolder 读写模式
我在 Spring Boot 应用程序 (v2.1.6.RELEASE) 中使用以下 Java 代码:
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();
}
但这会引发异常 com.sun.mail.iap.CommandFailedException: A7 NO Command received in Invalid state.
当有匹配的消息时.这似乎是 IMAP 文件夹默认设置为只读的问题.
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 IMAP folder being set as Read-only by default.
AbstractMailReceiver
类中有一个 onInit
保护方法,它以读写模式打开文件夹.我有尝试调用最终调用的
方法,但这给了我 ImapMailReceiver.afterPropertiesSet()
onInit一个外部接收器"......无法修改.
:
There is an onInit
protected method in 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 on how to set the folder in read-write mode?
P.S 在普通的 Java 代码中,我可以使用 inbox.open(Folder.READ_WRITE)
设置相同的内容,这有效.
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);
}
我收到以下错误:
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
编辑 2
如果我使用如下代码:
EDIT 2
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);
}
我收到以下错误:
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
声明显式接收器 bean 有效
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");
}
编辑
您不得在接收器规范上调用 get()
;框架需要对规范本身的引用才能正确初始化 bean.
You must not call get()
on the receiver spec; the framework needs a reference to the spec itself to properly initialize the bean(s).