我正在使用以下代码从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);
}
}
}
如果我发送纯文本电子邮件,处理程序将启动并记录:
INFO : Got message!
INFO : Subject: Lorem ipsum dolor sit amet
INFO : ContentType: text/plain; charset="utf-8"
INFO : Content:
INFO : Length: 0
如果我发送HTML电子邮件,处理程序将启动并记录:
INFO : Got message!
INFO : Subject: Lorem ipsum dolor sit amet
INFO : ContentType: text/html; charset="utf-8"
INFO : Content:
INFO : Length: 0
主题是正确的(标题也是正确的),但是对于普通电子邮件和HTML电子邮件,内容始终为空。
此外,我希望收到HTML的multipart
消息,而不仅仅是text/html
部分。实际上,如果我在电子邮件客户端中检查原始邮件,则会看到:
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部分并将其与所有原始标头一起传递给处理程序;仍然没有内容。
我做错什么了吗?
答案 0 :(得分:0)
尝试在simpleContent
上将true
设置为ImapMailReceiver
: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;
}
}
而不是急于获取:
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) {
我相信这也应该对您有所帮助。