我正在尝试过滤收到的邮件 - 如果邮件正文包含“github.com”,则应用程序将不会自动回复。所以我尝试为此编写IntegrationFlow(请参阅下面的代码)。我不知道如何处理它,因为看起来Transformer无法打开收件箱文件夹,这似乎是Java Mail API问题?
@Bean
open fun flow(): IntegrationFlow
{
return IntegrationFlows
.from("emailReceiveChannel")
.transform(transformer())
.filter("@messageFilter.containsDomainNames('payload')")
.handle(MessageHandler(MailServiceImpl(javaMailSender(), mailStore())))
.get()
}
其中transfomer()
是:
@Bean
@Transformer(inputChannel = "emailReceiveChannel", outputChannel = "outputChannel")
open fun transformer(): org.springframework.integration.transformer.Transformer
{
return MailToStringTransformer()
}
和messageFilter
:
@Component
class MessageFilter
{
@Filter
open fun containsDomainNames(messageBody: String): Boolean
{
return messageBody.contains("github.com") ||
messageBody.contains("trello.com") ||
messageBody.contains("bitbucket.com")
}
}
@Bean
@InboundChannelAdapter(autoStartup = "true", value = "emailReceiveChannel", poller = (arrayOf(Poller(fixedDelay = "10000", maxMessagesPerPoll = "10"))))
open fun mailReceivingMessageSource(mailReceiver: ImapMailReceiver): MailReceivingMessageSource
{
return MailReceivingMessageSource(mailReceiver)
}
@Bean
open fun pollingConsumer(): PollingConsumer
{
return PollingConsumer(emailReceiveChannel(), MessageHandler(MailServiceImpl(javaMailSender(), mailStore())))
}
@Bean
open fun outputChannel(): PollableChannel
{
return QueueChannel()
}
@Bean
open fun emailReceiveChannel(): PollableChannel
{
return QueueChannel(10)
}
堆栈跟踪:
2018-01-20 13:05:53.840 ERROR 8204 --- [ask-scheduler-6] o.s.integration.handler.LoggingHandler : org.springframework.integration.transformer.MessageTransformationException: failed to transform mail message; nested exception is javax.mail.FolderClosedException, failedMessage=GenericMessage [payload=org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@3f0ef4a2, headers={id=e9eff05d-e9af-70e4-73f7-c4d39c740291, timestamp=1516449953838}]
at org.springframework.integration.mail.transformer.AbstractMailMessageTransformer.transform(AbstractMailMessageTransformer.java:83)
at org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:89)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:131)
at org.springframework.integration.endpoint.PollingConsumer.handleMessage(PollingConsumer.java:129)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:271)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.lambda$run$0(AbstractPollingEndpoint.java:372)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.lambda$execute$0(ErrorHandlingTaskExecutor.java:53)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:51)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:366)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:83)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.mail.FolderClosedException
at com.sun.mail.imap.IMAPMessage.getProtocol(IMAPMessage.java:153)
at com.sun.mail.imap.IMAPBodyPart.loadHeaders(IMAPBodyPart.java:390)
at com.sun.mail.imap.IMAPBodyPart.getNonMatchingHeaderLines(IMAPBodyPart.java:371)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1536)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:948)
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:538)
at org.springframework.integration.mail.transformer.MailToStringTransformer.doTransform(MailToStringTransformer.java:62)
at org.springframework.integration.mail.transformer.AbstractMailMessageTransformer.transform(AbstractMailMessageTransformer.java:80)
... 19 more
答案 0 :(得分:1)
我在邮件附件项目中存在以下相同问题: https://github.com/spring-projects/spring-integration
我用
解决了<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-bom</artifactId>
<version>4.3.19.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
代替
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-bom</artifactId>
<version>5.1.3.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
编辑:5.1.3.RELEASE版本也可以,但有必要: mail:inbound-channel-adapter中的simple-content =“ true”
答案 1 :(得分:1)
此问题与集成库更新相对应,这就是为什么对该库进行降级是有帮助的,但是正确的方法是添加一个Java邮件属性:simpleContent。
我的代码段:
@Bean
public IntegrationFlow userEmailFlow(EmailProperties props,
EmailToUserTransformer emailToUserTransformer,
UserMessageHandler userMessageHandler) {
return IntegrationFlows
.from(Mail.imapInboundAdapter(props.getImapUrl())
.javaMailProperties(p -> p.put("mail.debug", "false")
.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory")
.put("mail.imap.socketFactory.fallback", "false")
.put("mail.store.protocol", "imaps"))
.shouldMarkMessagesAsRead(false)
.shouldDeleteMessages(false)
.simpleContent(true),
e -> e.poller(Pollers.fixedDelay(props.getPollRate())))
.transform(emailToUserTransformer)
.handle(userMessageHandler)
.get();
}