Spring Boot Mail send email using acces token











up vote
1
down vote

favorite












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










share|improve this question






















  • If the token is a JWT(I assume), then you can do it by adding a password claim in the token then decoding it and retrieving the password and move forward. There is no other way I believe you can do it since JavaMail has password property but no token verfication property
    – Anas
    Nov 19 at 15:21










  • @Anas access/refresh tokens are given by OAuth2 protocol. When user tries to authenticate gmail account my server getting authorization code and then I exchange for refresh/access token.
    – Dave
    Nov 19 at 16:00










  • Check the answer by bill, it has connecting to javamail through oauth access token
    – Anas
    Nov 20 at 10:24















up vote
1
down vote

favorite












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










share|improve this question






















  • If the token is a JWT(I assume), then you can do it by adding a password claim in the token then decoding it and retrieving the password and move forward. There is no other way I believe you can do it since JavaMail has password property but no token verfication property
    – Anas
    Nov 19 at 15:21










  • @Anas access/refresh tokens are given by OAuth2 protocol. When user tries to authenticate gmail account my server getting authorization code and then I exchange for refresh/access token.
    – Dave
    Nov 19 at 16:00










  • Check the answer by bill, it has connecting to javamail through oauth access token
    – Anas
    Nov 20 at 10:24













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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










share|improve this question













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







spring spring-boot javamail






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 13:58









Dave

104110




104110












  • If the token is a JWT(I assume), then you can do it by adding a password claim in the token then decoding it and retrieving the password and move forward. There is no other way I believe you can do it since JavaMail has password property but no token verfication property
    – Anas
    Nov 19 at 15:21










  • @Anas access/refresh tokens are given by OAuth2 protocol. When user tries to authenticate gmail account my server getting authorization code and then I exchange for refresh/access token.
    – Dave
    Nov 19 at 16:00










  • Check the answer by bill, it has connecting to javamail through oauth access token
    – Anas
    Nov 20 at 10:24


















  • If the token is a JWT(I assume), then you can do it by adding a password claim in the token then decoding it and retrieving the password and move forward. There is no other way I believe you can do it since JavaMail has password property but no token verfication property
    – Anas
    Nov 19 at 15:21










  • @Anas access/refresh tokens are given by OAuth2 protocol. When user tries to authenticate gmail account my server getting authorization code and then I exchange for refresh/access token.
    – Dave
    Nov 19 at 16:00










  • Check the answer by bill, it has connecting to javamail through oauth access token
    – Anas
    Nov 20 at 10:24
















If the token is a JWT(I assume), then you can do it by adding a password claim in the token then decoding it and retrieving the password and move forward. There is no other way I believe you can do it since JavaMail has password property but no token verfication property
– Anas
Nov 19 at 15:21




If the token is a JWT(I assume), then you can do it by adding a password claim in the token then decoding it and retrieving the password and move forward. There is no other way I believe you can do it since JavaMail has password property but no token verfication property
– Anas
Nov 19 at 15:21












@Anas access/refresh tokens are given by OAuth2 protocol. When user tries to authenticate gmail account my server getting authorization code and then I exchange for refresh/access token.
– Dave
Nov 19 at 16:00




@Anas access/refresh tokens are given by OAuth2 protocol. When user tries to authenticate gmail account my server getting authorization code and then I exchange for refresh/access token.
– Dave
Nov 19 at 16:00












Check the answer by bill, it has connecting to javamail through oauth access token
– Anas
Nov 20 at 10:24




Check the answer by bill, it has connecting to javamail through oauth access token
– Anas
Nov 20 at 10:24












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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



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






share|improve this answer





















  • Good answer, it helped me.
    – Dave
    Nov 20 at 13:21










  • Is it good to hold opening one session when application is started and then don't close until application will be shutdown for different emails? Because now it opens session inside method and sending, after finishing as I understand it closes session and it will be so every time.
    – Dave
    Nov 20 at 13:26












  • A JavaMail Session can be long lived, but a Transport or Store that has been connected to a server will keep open a network socket connection, which might be an issue. And the server can close the connection at any time, so you need to be prepared to handle that.
    – Bill Shannon
    Nov 20 at 20:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376248%2fspring-boot-mail-send-email-using-acces-token%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










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



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






share|improve this answer





















  • Good answer, it helped me.
    – Dave
    Nov 20 at 13:21










  • Is it good to hold opening one session when application is started and then don't close until application will be shutdown for different emails? Because now it opens session inside method and sending, after finishing as I understand it closes session and it will be so every time.
    – Dave
    Nov 20 at 13:26












  • A JavaMail Session can be long lived, but a Transport or Store that has been connected to a server will keep open a network socket connection, which might be an issue. And the server can close the connection at any time, so you need to be prepared to handle that.
    – Bill Shannon
    Nov 20 at 20:57















up vote
2
down vote



accepted










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



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






share|improve this answer





















  • Good answer, it helped me.
    – Dave
    Nov 20 at 13:21










  • Is it good to hold opening one session when application is started and then don't close until application will be shutdown for different emails? Because now it opens session inside method and sending, after finishing as I understand it closes session and it will be so every time.
    – Dave
    Nov 20 at 13:26












  • A JavaMail Session can be long lived, but a Transport or Store that has been connected to a server will keep open a network socket connection, which might be an issue. And the server can close the connection at any time, so you need to be prepared to handle that.
    – Bill Shannon
    Nov 20 at 20:57













up vote
2
down vote



accepted







up vote
2
down vote



accepted






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



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






share|improve this answer












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



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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 20:53









Bill Shannon

23.1k42932




23.1k42932












  • Good answer, it helped me.
    – Dave
    Nov 20 at 13:21










  • Is it good to hold opening one session when application is started and then don't close until application will be shutdown for different emails? Because now it opens session inside method and sending, after finishing as I understand it closes session and it will be so every time.
    – Dave
    Nov 20 at 13:26












  • A JavaMail Session can be long lived, but a Transport or Store that has been connected to a server will keep open a network socket connection, which might be an issue. And the server can close the connection at any time, so you need to be prepared to handle that.
    – Bill Shannon
    Nov 20 at 20:57


















  • Good answer, it helped me.
    – Dave
    Nov 20 at 13:21










  • Is it good to hold opening one session when application is started and then don't close until application will be shutdown for different emails? Because now it opens session inside method and sending, after finishing as I understand it closes session and it will be so every time.
    – Dave
    Nov 20 at 13:26












  • A JavaMail Session can be long lived, but a Transport or Store that has been connected to a server will keep open a network socket connection, which might be an issue. And the server can close the connection at any time, so you need to be prepared to handle that.
    – Bill Shannon
    Nov 20 at 20:57
















Good answer, it helped me.
– Dave
Nov 20 at 13:21




Good answer, it helped me.
– Dave
Nov 20 at 13:21












Is it good to hold opening one session when application is started and then don't close until application will be shutdown for different emails? Because now it opens session inside method and sending, after finishing as I understand it closes session and it will be so every time.
– Dave
Nov 20 at 13:26






Is it good to hold opening one session when application is started and then don't close until application will be shutdown for different emails? Because now it opens session inside method and sending, after finishing as I understand it closes session and it will be so every time.
– Dave
Nov 20 at 13:26














A JavaMail Session can be long lived, but a Transport or Store that has been connected to a server will keep open a network socket connection, which might be an issue. And the server can close the connection at any time, so you need to be prepared to handle that.
– Bill Shannon
Nov 20 at 20:57




A JavaMail Session can be long lived, but a Transport or Store that has been connected to a server will keep open a network socket connection, which might be an issue. And the server can close the connection at any time, so you need to be prepared to handle that.
– Bill Shannon
Nov 20 at 20:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376248%2fspring-boot-mail-send-email-using-acces-token%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen