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
spring spring-boot javamail
add a comment |
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
spring spring-boot javamail
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 sinceJavaMail
haspassword
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
add a comment |
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
spring spring-boot javamail
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
spring spring-boot javamail
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 sinceJavaMail
haspassword
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
add a comment |
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 sinceJavaMail
haspassword
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
The use of OAUTH2 with JavaMail is explained on the JavaMail project page.
Also, you should fix these common mistakes in your code.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
haspassword
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