Spring 集成电子邮件 pop3 入站适配器未 working/starting

Spring integration email pop3 inbound adapter not working/starting

使用 java spring 集成编写了以下代码来读取来自 gmail 的电子邮件。 根据日志,似乎已形成与 gmail 的连接,但在新电子邮件上它不读取或不进入 handle() 方法。请帮忙。

o.s.i.m.AbstractMailReceiver - 试图从文件夹 [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();
    }
}

问题出在这里:

.maxFetchSize(1)

默认情况下,AbstractMailReceiver 会尝试从邮箱中获取所有邮件。而且看起来要花很多时间。

另一个问题是 .headerMapper(mailHeaderMapper() Mail.pop3InboundAdapter 不会产生 Message 而是 byte[]。因此,您的 .handle((payload, header) -> logMail(payload)) 不仅在流程末尾的 request-reply 定义中是错误的,而且像这样的 ClassCast 也失败了,因为您期望的类型不是为您生成的:

Caused by: java.lang.ClassCastException: class [B cannot be cast to class org.springframework.messaging.Message ([B is in module java.base of loader 'bootstrap'; org.springframework.messaging.Message is in unnamed module of loader 'app')
at com.firm.demo.EmailReceiver.logMail(EmailReceiver.java:59)
at com.firm.demo.EmailReceiver.lambda$pop3MailFlow(EmailReceiver.java:53)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)

嗯,不:最好是说您忽略了一个事实,即此签名 .handle((payload, header) -> logMail(payload)) 处理的是 payload 而不是整个消息。所以,您对 logMail() 的期望是错误的。它必须是 byte[]。并考虑在流程结束时在那里有一个 one-way 处理程序。

更新

工作代码是这样的:

@Log4j2
@Configuration
@EnableIntegration
public class EmailReceiver {

    @Autowired
    private PollableChannel pop3Channel;

    private MailReceiver receiver;


    @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", "false");
                                    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);
                                })
                                .maxFetchSize(1)
                                .headerMapper(mailHeaderMapper()),
                        e -> e.poller(Pollers.fixedRate(5000).maxMessagesPerPoll(1)))
                .handle(this, "logMail")
                .get();
    }


    public void logMail(String payload) {
        log.info("*******Email[TEST]********* \n" + payload);
    }

    @Bean
    public HeaderMapper<MimeMessage> mailHeaderMapper() {
        return new DefaultMailHeaderMapper();
    }


}