Kuan Kit Ng
Kuan Kit Ng

Reputation: 13

Is Spring Integration Mail IMAP support OAuth2?

I'm using Spring integration mail for imap idle events. I want to implement a IMAP gmail client using the token instead of using username and password.

Is Spring Integration Mail IMAP able to connect to gmail store with access token?

Here's the code I used

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();

Upvotes: 1

Views: 3045

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121427

Well, sounds like we just need to perform some REST request to get a token and together with some Java Mail properties set it into the URL instead of that [password] placeholder: 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");

For this purpose it sounds like you should use dynamic flow registration, so you can perform token request first and then register that Mail.imapIdleAdapter() based on the result of the first request: https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl-runtime-flows

Upvotes: 1

Related Questions