Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Mail send email using acces token

I have simple mail sending functionality in project which configured in one bean.

@Bean
public JavaMailSender javaMailSender() {
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

    Properties properties = new Properties();
    properties.setProperty("mail.smtp.auth", "false");
    properties.setProperty("mail.smtp.socketFactory.port", "465");
    properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    properties.setProperty("smtp.socketFactory.fallback", "false");
    properties.setProperty("mail.smtp.starttls.enable", "true");
    properties.setProperty("mail.smtp.starttls.required", "true");

    javaMailSender.setHost("smtp.gmail.com");
    javaMailSender.setProtocol("smtp");
    javaMailSender.setUsername("username");
    javaMailSender.setPassword("password");
    javaMailSender.setJavaMailProperties(properties);

    return javaMailSender;
}

and it works great.

Now I want to add functionality for sending emails via accessToken/refreshToken of specific email.

How to do it? What should I extend in my bean or add another bean for sending with token? I couldn't find some example which is full explained. As I understand I should add setFrom() and in setPassword() put accessToken

like image 533
Dave Avatar asked Dec 17 '25 17:12

Dave


1 Answers

The use of OAUTH2 with JavaMail is explained on the JavaMail project page.

Also, you should fix these common mistakes in your code.

like image 73
Bill Shannon Avatar answered Dec 20 '25 10:12

Bill Shannon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!