Spring集成电子邮件pop3入站适配器不工作/启动

发布于 2025-01-11 21:13:15 字数 2572 浏览 6 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍景流年 2025-01-18 21:13:15

问题就在这里:

.maxFetchSize(1)

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

另一个问题是 .headerMapper(mailHeaderMapper()Mail.pop3InboundAdapter 不会产生 Message 而是产生 byte[]<因此,您的 .handle((payload, header) -> logMail(payload)) 不仅在流程结束时的请求-回复定义中出现问题,而且还会失败像这样的 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$2(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[]。无论如何,在流程结束时有一个单向处理程序

更新

工作代码如下:

@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();
    }


}

The problem is here:

.maxFetchSize(1)

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() a Mail.pop3InboundAdapter does not produce a Message but rather byte[]. So, your .handle((payload, header) -> logMail(payload)) not only bad by the request-reply definition in the end of flow, but also fails with ClassCast like this, because you expect the type which is not produced for you:

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$2(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)

Well, no: better to say you are missing the fact that this signature .handle((payload, header) -> logMail(payload)) deals with the payload not the whole message. So, your expectations in the logMail() are wrong. It has to be byte[]. And consider to have a one-way handler over there in the end of flow anyway.

UPDATE

The working code is like this:

@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();
    }


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文