ImapIdleChannelAdapter 获取不到消息内容
ImapIdleChannelAdapter doesn't get message content
我正在使用此代码从 IMAP 服务器读取邮件:
@EnableIntegration
public class MailIntegration implements HasLogger {
@Bean
public ImapIdleChannelAdapter messageChannel(ImapMailReceiver receiver) {
var receiver = new ImapMailReceiver("imaps://...");
var adapter = new ImapIdleChannelAdapter(receiver);
adapter.setOutputChannelName("imapChannel");
return adapter;
}
@ServiceActivator(inputChannel = "imapChannel")
public void handleMessage(MimeMessage message) {
getLogger().info("Got message!");
var subject = message.getSubject();
getLogger().info("Subject: {}", subject);
var contentType = message.getContentType();
getLogger().info("ContentType: {}", contentType);
var content = message.getContent();
if (content instanceof String) {
var text = (String) content;
getLogger().info("Content: {}", text);
getLogger().info("Length: {}", text.length());
} else {
getLogger().info("Other content: {}", content);
}
}
}
如果我发送纯文本 e-mail,处理程序将启动并记录:
INFO : Got message!
INFO : Subject: Lorem ipsum dolor sit amet
INFO : ContentType: text/plain; charset="utf-8"
INFO : Content:
INFO : Length: 0
如果我发送 HTML e-mail,处理程序将启动并记录:
INFO : Got message!
INFO : Subject: Lorem ipsum dolor sit amet
INFO : ContentType: text/html; charset="utf-8"
INFO : Content:
INFO : Length: 0
主题是正确的(headers 也是如此)但是对于普通内容和 HTML e-mail 两者,内容总是空的。
此外,我希望收到 HTML 的 multipart
消息,而不仅仅是 text/html
部分。事实上,如果我在 e-mail 客户端中检查原始消息,我会看到:
From: Giovanni Lovato <giovanni.lovato@...>
To: Test <test@...>
Subject: Lorem ipsum dolor sit amet
... lots of other header lines ...
Content-type: multipart/alternative; boundary="B_3642854791_1171496246"
> This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
--B_3642854791_1171496246
Content-type: text/plain; charset="UTF-8"
Content-transfer-encoding: quoted-printable
Lorem ipsum dolor sit amet.
--B_3642854791_1171496246
Content-type: text/html; charset="UTF-8"
Content-transfer-encoding: quoted-printable
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>Lorem ipsum dolor sit amet.</p>
</body>
</html>
--B_3642854791_1171496246--
所以看起来 ImapIdleChannelAdapter
已经在提取 HTML 部分并将其传递给处理程序,所有原始 headers;不过仍然没有内容。
我是不是做错了什么?
尝试在 ImapMailReceiver
上将 simpleContent
设置为 true
:https://docs.spring.io/spring-integration/docs/current/reference/html/#mail-inbound
如果为真,邮件正文内容按需获取:
@Override
public Object getContent() throws IOException, MessagingException {
if (AbstractMailReceiver.this.simpleContent) {
return super.getContent();
}
else {
return this.content;
}
}
而不是 eager fetch 否则:
IntegrationMimeMessage(MimeMessage source) throws MessagingException {
super(source);
this.source = source;
if (AbstractMailReceiver.this.simpleContent) {
this.content = null;
}
else {
Object complexContent;
try {
complexContent = source.getContent();
}
catch (IOException e) {
complexContent = "Unable to extract content; see logs: " + e.getMessage();
AbstractMailReceiver.this.logger.error("Failed to extract content from " + source, e);
}
this.content = complexContent;
}
}
在版本5.2
中我们引入了:
/**
* Configure a {@code boolean} flag to close the folder automatically after a fetch (default) or
* populate an additional {@link IntegrationMessageHeaderAccessor#CLOSEABLE_RESOURCE} message header instead.
* It is the downstream flow's responsibility to obtain this header and call its {@code close()} whenever
* it is necessary.
* <p> Keeping the folder open is useful in cases where communication with the server is needed
* when parsing multipart content of the email with attachments.
* <p> The {@link #setSimpleContent(boolean)} and {@link #setHeaderMapper(HeaderMapper)} options are not
* affected by this flag.
* @param autoCloseFolder {@code false} do not close the folder automatically after a fetch.
* @since 5.2
*/
public void setAutoCloseFolder(boolean autoCloseFolder) {
我相信这个也应该对你有帮助。
我正在使用此代码从 IMAP 服务器读取邮件:
@EnableIntegration
public class MailIntegration implements HasLogger {
@Bean
public ImapIdleChannelAdapter messageChannel(ImapMailReceiver receiver) {
var receiver = new ImapMailReceiver("imaps://...");
var adapter = new ImapIdleChannelAdapter(receiver);
adapter.setOutputChannelName("imapChannel");
return adapter;
}
@ServiceActivator(inputChannel = "imapChannel")
public void handleMessage(MimeMessage message) {
getLogger().info("Got message!");
var subject = message.getSubject();
getLogger().info("Subject: {}", subject);
var contentType = message.getContentType();
getLogger().info("ContentType: {}", contentType);
var content = message.getContent();
if (content instanceof String) {
var text = (String) content;
getLogger().info("Content: {}", text);
getLogger().info("Length: {}", text.length());
} else {
getLogger().info("Other content: {}", content);
}
}
}
如果我发送纯文本 e-mail,处理程序将启动并记录:
INFO : Got message!
INFO : Subject: Lorem ipsum dolor sit amet
INFO : ContentType: text/plain; charset="utf-8"
INFO : Content:
INFO : Length: 0
如果我发送 HTML e-mail,处理程序将启动并记录:
INFO : Got message!
INFO : Subject: Lorem ipsum dolor sit amet
INFO : ContentType: text/html; charset="utf-8"
INFO : Content:
INFO : Length: 0
主题是正确的(headers 也是如此)但是对于普通内容和 HTML e-mail 两者,内容总是空的。
此外,我希望收到 HTML 的 multipart
消息,而不仅仅是 text/html
部分。事实上,如果我在 e-mail 客户端中检查原始消息,我会看到:
From: Giovanni Lovato <giovanni.lovato@...>
To: Test <test@...>
Subject: Lorem ipsum dolor sit amet
... lots of other header lines ...
Content-type: multipart/alternative; boundary="B_3642854791_1171496246"
> This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
--B_3642854791_1171496246
Content-type: text/plain; charset="UTF-8"
Content-transfer-encoding: quoted-printable
Lorem ipsum dolor sit amet.
--B_3642854791_1171496246
Content-type: text/html; charset="UTF-8"
Content-transfer-encoding: quoted-printable
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>Lorem ipsum dolor sit amet.</p>
</body>
</html>
--B_3642854791_1171496246--
所以看起来 ImapIdleChannelAdapter
已经在提取 HTML 部分并将其传递给处理程序,所有原始 headers;不过仍然没有内容。
我是不是做错了什么?
尝试在 ImapMailReceiver
上将 simpleContent
设置为 true
:https://docs.spring.io/spring-integration/docs/current/reference/html/#mail-inbound
如果为真,邮件正文内容按需获取:
@Override
public Object getContent() throws IOException, MessagingException {
if (AbstractMailReceiver.this.simpleContent) {
return super.getContent();
}
else {
return this.content;
}
}
而不是 eager fetch 否则:
IntegrationMimeMessage(MimeMessage source) throws MessagingException {
super(source);
this.source = source;
if (AbstractMailReceiver.this.simpleContent) {
this.content = null;
}
else {
Object complexContent;
try {
complexContent = source.getContent();
}
catch (IOException e) {
complexContent = "Unable to extract content; see logs: " + e.getMessage();
AbstractMailReceiver.this.logger.error("Failed to extract content from " + source, e);
}
this.content = complexContent;
}
}
在版本5.2
中我们引入了:
/**
* Configure a {@code boolean} flag to close the folder automatically after a fetch (default) or
* populate an additional {@link IntegrationMessageHeaderAccessor#CLOSEABLE_RESOURCE} message header instead.
* It is the downstream flow's responsibility to obtain this header and call its {@code close()} whenever
* it is necessary.
* <p> Keeping the folder open is useful in cases where communication with the server is needed
* when parsing multipart content of the email with attachments.
* <p> The {@link #setSimpleContent(boolean)} and {@link #setHeaderMapper(HeaderMapper)} options are not
* affected by this flag.
* @param autoCloseFolder {@code false} do not close the folder automatically after a fetch.
* @since 5.2
*/
public void setAutoCloseFolder(boolean autoCloseFolder) {
我相信这个也应该对你有帮助。