gpt4 book ai didi

java - 如何使用 spring 集成正确获取入站消息 header

转载 作者:行者123 更新时间:2023-11-30 07:15:40 27 4
gpt4 key购买 nike

我收到了带有 spring 的电子邮件,到目前为止有一个非常基本的脚本

ApplicationContext ac = new ClassPathXmlApplicationContext("imap.xml");
DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);
inputChannel.subscribe(message -> {
System.out.println(message.getHeaders());
System.out.println(message.getPayload());

MessageHeaders headers = message.getHeaders();
String from = (String) headers.get("mail_from");
});

根据documentation我以为 header 会自动解析,但是我从第一个 System.out.println(); 获得的 header 只是

{id=c65f55aa-c611-71ee-c56d-6bf13c6f71d0, timestamp=1468869891279}

第二个输出(对于 getPayload() )是

org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@6af5615d

from输出null ...

然后我尝试使用 MailToStringTransformer

MailToStringTransformer a = transformer();
a.setCharset("utf-8");
System.out.println(a.transform(message));

它输出有效负载和我期望的所有 header ,但(自然地)作为字符串。

我需要做什么才能获取对象中的消息标题和文本?

最佳答案

不确定您指的是哪个文档,但当前的 4.3版本有这个:

By default, the payload of messages produced by the inbound adapters is the raw MimeMessage; you can interrogate the headers and content using that object. Starting with version 4.3, you can provide a HeaderMapper<MimeMessage> to map the headers to MessageHeaders; for convenience, a DefaultMailHeaderMapper is provided for this purpose.

下面一点:

When you do not provide a header mapper, the message payload is the MimeMessage presented by javax.mail. The framework provides a MailToStringTransformer...

如果您需要对映射进行一些自定义,您始终可以提供自己的 HeaderMapper<MimeMessage>实现或DefaultMailHeaderMapper扩展名。

关于java - 如何使用 spring 集成正确获取入站消息 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38444803/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com