如何避免使用 spring 集成邮件加载电子邮件内容

How to avoid loading email's content using spring integration mail

我正在使用 spring 集成邮件(5.3.1 版本) 我有流量:

    IntegrationFlows
    .from(Mail
        .imapIdleAdapter(imapAdapter)
    )
    .filter()
    .filter()
    ...
    .filter()
    .handle(service1)
    .get();

我想在服务 1 中加载电子邮件内容。 在通过所有过滤器之前,我不想加载电子邮件的内容。 我的过滤器只需要知道电子邮件 headers.

我尝试使用 DefaultMailHeaderMapper,但电子邮件的内容还是被加载了。我可以使用“mail.debug”=true.

在日志中看到它

我正在调试,根据 AbstractMailReceiver#receive 的来源,MimeMessage 的内容将始终被加载,因为例如 new IntegrationMimeMessage() 使用加载内容的 MimeMessage(MimeMessage message) 构造函数。

有没有办法配置邮件适配器不加载电子邮件的内容?

谢谢!

参见文档:https://docs.spring.io/spring-integration/docs/current/reference/html/mail.html#mail-inbound。特别是这部分:

tarting with version 5.2, the autoCloseFolder option is provided on the mail receiver. Setting it to false doesn’t close the folder automatically after a fetch, but instead an IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE header (see MessageHeaderAccessor API for more information) is populated into every message to producer from the channel adapter.

然后看下一节:https://docs.spring.io/spring-integration/docs/current/reference/html/mail.html#mail-mapping

因此,为避免急于加载内容,您应该放弃 header 映射器并且不要自动关闭文件夹。这样整个 MimeMessage 作为有效载荷发送。您可能无法针对 header 执行过滤逻辑,因为未获取消息内容,因此我们不知道 MimeMessage 中有哪些 header。但是,您可以尝试从过滤器访问它们,但已经针对有效负载 - 而不是 headers.