Spring集成电子邮件pop3入站适配器不工作/启动
使用java spring集成编写了以下代码来从gmail读取电子邮件。 根据日志,似乎与 gmail 的连接已形成,但在新电子邮件上,它不读取或不进入 handle() 方法。请帮忙。
osimAbstractMailReceiver - 尝试从文件夹 [INBOX] 接收邮件
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.mail.MailReceiver;
import org.springframework.integration.mail.dsl.Mail;
import org.springframework.integration.mail.support.DefaultMailHeaderMapper;
import org.springframework.integration.mapping.HeaderMapper;
import org.springframework.messaging.Message;
import org.springframework.messaging.PollableChannel;
import javax.mail.internet.MimeMessage;
@Log4j2
@Configuration
@EnableIntegration
public class EmailReceiver {
@Autowired
private PollableChannel pop3Channel;
@Bean
public PollableChannel receivedChannel() {
return new QueueChannel();
}
@Bean
public IntegrationFlow pop3MailFlow() {
return IntegrationFlows
.from(Mail.pop3InboundAdapter("pop.gmail.com", 995, "userName", "password")
.javaMailProperties(p -> {
p.put("mail.debug", "true");
p.put("mail.pop3.socketFactory.fallback", "false");
p.put("mail.pop3.port", 995);
p.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
p.put("mail.pop3.socketFactory.port", 995);
})
.headerMapper(mailHeaderMapper()),
e -> e.poller(Pollers.fixedRate(5000).maxMessagesPerPoll(1)))
.handle((payload, header) -> logMail(payload))
.get();
}
public Message logMail(Object payload) {
Message message = (Message)payload;
log.info("*******Email[TEST]********* ", payload);
return message;
}
@Bean
public HeaderMapper<MimeMessage> mailHeaderMapper() {
return new DefaultMailHeaderMapper();
}
}
With java spring integration written the below code to read the email from gmail.
As per logs seems the connection with gmail is formed, but on new email its not reading or not going into handle() method. Please help.
o.s.i.m.AbstractMailReceiver - attempting to receive mail from folder [INBOX]
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.mail.MailReceiver;
import org.springframework.integration.mail.dsl.Mail;
import org.springframework.integration.mail.support.DefaultMailHeaderMapper;
import org.springframework.integration.mapping.HeaderMapper;
import org.springframework.messaging.Message;
import org.springframework.messaging.PollableChannel;
import javax.mail.internet.MimeMessage;
@Log4j2
@Configuration
@EnableIntegration
public class EmailReceiver {
@Autowired
private PollableChannel pop3Channel;
@Bean
public PollableChannel receivedChannel() {
return new QueueChannel();
}
@Bean
public IntegrationFlow pop3MailFlow() {
return IntegrationFlows
.from(Mail.pop3InboundAdapter("pop.gmail.com", 995, "userName", "password")
.javaMailProperties(p -> {
p.put("mail.debug", "true");
p.put("mail.pop3.socketFactory.fallback", "false");
p.put("mail.pop3.port", 995);
p.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
p.put("mail.pop3.socketFactory.port", 995);
})
.headerMapper(mailHeaderMapper()),
e -> e.poller(Pollers.fixedRate(5000).maxMessagesPerPoll(1)))
.handle((payload, header) -> logMail(payload))
.get();
}
public Message logMail(Object payload) {
Message message = (Message)payload;
log.info("*******Email[TEST]********* ", payload);
return message;
}
@Bean
public HeaderMapper<MimeMessage> mailHeaderMapper() {
return new DefaultMailHeaderMapper();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题就在这里:
默认情况下,
AbstractMailReceiver
尝试从邮箱中获取所有消息。而且看起来需要很多时间。另一个问题是
.headerMapper(mailHeaderMapper()
的Mail.pop3InboundAdapter
不会产生Message
而是产生byte[]<因此,您的
.handle((payload, header) -> logMail(payload))
不仅在流程结束时的请求-回复定义中出现问题,而且还会失败像这样的ClassCast
,因为您期望的类型不是为您生成的:嗯,不:更好地说您错过了这个签名
.handle((payload, header) - > logMail(payload))
处理payload
而不是整个消息,因此,您对logMail()
的期望一定是错误的。byte[]
。无论如何,在流程结束时有一个单向处理程序更新
工作代码如下:
The problem is here:
By default the
AbstractMailReceiver
tries to fetch all the messages from the mail box. And looks like it takes a lot of time.Another problem that with the
.headerMapper(mailHeaderMapper()
aMail.pop3InboundAdapter
does not produce aMessage
but ratherbyte[]
. So, your.handle((payload, header) -> logMail(payload))
not only bad by the request-reply definition in the end of flow, but also fails withClassCast
like this, because you expect the type which is not produced for you:Well, no: better to say you are missing the fact that this signature
.handle((payload, header) -> logMail(payload))
deals with thepayload
not the whole message. So, your expectations in thelogMail()
are wrong. It has to bebyte[]
. And consider to have a one-way handler over there in the end of flow anyway.UPDATE
The working code is like this: