使用带有文件附件的 gmail 消息的 Spring Integration 流程,例如

问题描述 投票:0回答:1

Spring Integration 5.x 在这里。我正在尝试写一个

IntegrationFlow

  1. 阅读来自 Gmail 帐户的电子邮件。如果电子邮件不包含文件附件,它们将被忽略并简单地标记为“已读”。否则……
  2. 对于每封带有附件的电子邮件,以及其中的每一个附件,我需要将附件放在
    Message<?>
    正文中(作为
    byte[]
    或类似的),我需要将消息发送到名为
     的 bean 处理程序documentRegistrar
    。到目前为止,我最好的尝试是:
@Configuration
public class GmailIntegrationConfiguration {

    private String emailHost;
    private String emailUsername;
    private String emailPassword;
    private DocumentRegistrar documentRegistrar;

    @Autowired
    public GmailIntegrationConfiguration(
            @Value("${email.host}") String emailHost,
            @Value("${email.username}") String emailUsername,
            @Value("${email.password}") String emailPassword,
            DocumentRegistrar documentRegistrar) {
        this.emailHost = emailHost;
        this.emailUsername = emailUsername;
        this.emailPassword = emailPassword;
        this.awsAccessKey = awsAccessKey;
    }

    @Bean
    public IntegrationFlow gmailIntegrationFlow() {
        return IntegrationFlows.from(emailAdapterSpec ->
            Mail.imapInboundAdapter("imap://imapuser:pw@localhost:993/INBOX")
                    .javaMailProperties(p -> p.put("mail.imap.ssl.enable", "true"))
                    .autoCloseFolder(false))
                .channel(MessageChannels.queue())
                .handle(documentRegistrar)
            .get();
    }

}

Mail.imapInboundAdapter
createImapAdapterSpec
的代码正是我能够使用 Spring Integration 文档和一些谷歌搜索一起 Frankenstein 的代码,但它们都充满了编译器错误。

任何人都可以在这里用一个具体的工作示例帮助我完成终点线吗?具体来说,

Mail.imapInboundAdapter
没有
port
user
password
等构建器方法,那么我将如何配置所有这些方法?并且文件附件过滤器肯定是错误的,因为 Spring 似乎没有定义
MimeBodyPart
类型(根本)。在此先感谢您提供的所有帮助!

gmail spring-integration jakarta-mail
1个回答
1
投票

不确定是什么驱使你采用这种配置风格,因为从

 Mail.imapInboundAdapter()
API 可以清楚地看出没有这些道具。 只有这个:

public static ImapMailInboundChannelAdapterSpec imapInboundAdapter(String url) {

所以,您需要从这些部分构建一个 URL。

此样本的风格:

Mail.imapInboundAdapter("imap://imapuser:pw@localhost:993/INBOX")

是的,文件夹需要作为 IMAP URL 的一部分出现。

查看

.autoCloseFolder(false)
选项及其 Javadocs 以了解要在下游解析的多部分邮件:

/**
 * 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) {

如果您不提供

HeaderMapper
,则使用
.autoCloseFolder(false)
,整个
MimeMessage
将在有效负载中提供。通道适配器不确定邮件消息中是否有某些附件。更重要的是,即使正文中只有一个简单的部分,Gmail 似乎总是生成多部分消息。

您可能需要将解析和过滤逻辑下推到您的

documentRegistrar
并且不要忘记不时关闭文件夹:https://docs.spring.io/spring-integration/docs/current/参考/html/mail.html#mail-inbound

© www.soinside.com 2019 - 2024. All rights reserved.