首页 > 解决方案 > ImapIdleChannelAdapter 没有得到消息内容

问题描述

我正在使用此代码从 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 电子邮件,内容始终为空。

此外,我希望收到multipartHTML 的消息,而不仅仅是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 部分并将其传递给处理程序,以及所有原始标题;但是,仍然没有内容。

难道我做错了什么?

标签: javaspringspring-integrationimapmime

解决方案


尝试设置为simpleContent:https: //docs.spring.io/spring-integration/docs/current/reference/html/#mail-inboundtrueImapMailReceiver

当这是真的时,邮件正文的内容是按需获取的:

@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) {

我相信这个也应该对你有所帮助。


推荐阅读