Spring Integration Mail IMAP 是否支持 OAuth2?

Is Spring Integration Mail IMAP support OAuth2?

我正在为 imap 空闲事件使用 Spring 集成邮件。我想使用令牌而不是使用用户名和密码来实现 IMAP gmail 客户端。

Spring Integration Mail IMAP 是否能够使用访问令牌连接到 gmail 存储?

这是我使用的代码

IntegrationFlow flow = IntegrationFlows
            .from(Mail.imapIdleAdapter("imaps://[username]:[password]@imap.gmail.com:993/inbox")
                    .javaMailProperties(p -> p
                            .put("mail.debug", "false")
                    .autoStartup(false)
                    .shouldReconnectAutomatically(true)
                    .userFlag("testSIUserFlag")
                    .headerMapper(new DefaultMailHeaderMapper()))
            .handle((Message<?> message) -> {
                logger.info("message: " + message);
            })
            .get();

嗯,听起来我们只需要执行一些 REST 请求来获取令牌,并与一些 Java 邮件属性一起将其设置到 URL 而不是那个 [password] 占位符: https://kgiann78.github.io/java/gmail/2017/03/16/JavaMail-send-mail-at-Google-with-XOAUTH2.html

 Properties props = new Properties();
 props.put("mail.imap.ssl.enable", "true"); // required for Gmail
 props.put("mail.imap.sasl.enable", "true");
 props.put("mail.imap.sasl.mechanisms", "XOAUTH2");
 props.put("mail.imap.auth.login.disable", "true");
 props.put("mail.imap.auth.plain.disable", "true");

为此,听起来您应该使用动态流注册,因此您可以先执行令牌请求,然后根据第一个请求的结果注册 Mail.imapIdleAdapter()https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl-runtime-flows