Using Gmail as SMTP server from Java, Spring Boot apps
Gmail users can use Gmail’s SMTP server smtp.gmail.com to send emails from their Spring Boot apps. For this let us do some setup in the app:
- Provide SMTP connection properties in the application.properties file:1234567
spring.mail.host=smtp.gmail.com
spring.mail.username=<your gmail/google app email>
spring.mail.password=*****
spring.mail.port=
587
spring.mail.properties.mail.smtp.starttls.enable=
true
spring.mail.properties.mail.smtp.auth=
true
spring.mail.properties.mail.smtp.starttls.required=
true
- Use Spring Boot Email tools library – which is a wrapper over Spring Boot Email starter library. Add the following in your pom.xml:12345
<
dependency
>
<
groupId
>it.ozimov</
groupId
>
<
artifactId
>spring-boot-email-core</
artifactId
>
<
version
>0.6.3</
version
>
</
dependency
>
- Annotation your application’s main class (i.e class annotated with @SpringBootApplication) with @EnableEmailTools:1234567
@SpringBootApplication
@EnableEmailTools
public
class
EmailApplication {
public
static
void
main(String[] args){
SpringApplication.run(EmailApplication.
class
, args);
}
}
- Let’s write a test which uses it.ozimov.springboot.mail.service.EmailService bean to send an email:01020304050607080910111213141516171819202122
@RunWith
(SpringRunner.
class
)
@SpringBootTest
public
class
EmailServiceTest {
@Autowired
it.ozimov.springboot.mail.service.EmailService emailService;
@Value
(
"${spring.mail.username}"
) String fromEmail;
@Test
public
void
testSendEmail()
throws
UnsupportedEncodingException {
User user =
new
User();
user.setEmail(
"sanaulla123@gmail.com"
);
user.setDisplayName(
"Mohamed Sanaulla"
);
final
Email email = DefaultEmail.builder()
.from(
new
InternetAddress(fromEmail,
"From Name"
))
.to(Lists.newArrayList(
new
InternetAddress(
user.getEmail(), user.getDisplayName())))
.subject(
"Testing email"
)
.body(
"Testing body ..."
)
.encoding(
"UTF-8"
).build();
emailService.send(email);
}
}
If all is well, you should receive an email in your inbox.
But all was not well when I tried the above code and the issue I faced was the following exception:
1 2 3 4 5 6 7 8 9 | Caused by: javax.mail.AuthenticationFailedException: 534 - 5.7 . 14 <https: //accounts.google.com/signin/continue?sarp=1≻c=1&plt=AKgnsbs2 534 - 5.7 . 14 tEY84q9p029iw1YKFy_d8O1vYNwHLixZUNHZlZbIqZki9a-EBfcUTPIenD2i6pN704O_7S 534 - 5.7 . 14 DK4FC- 8 -l1K1gU537F4UxjN4v4_txZ5pqxEA8ATwDhmOBzvxAYApfJTQjHL1yhHouwbhGO 534 - 5.7 . 14 LhOzSAB6Va6u-enaDfcv73dEgv1TT4b19dBfgzIkOoz_7nJ3i-LwWxZqIRyxOEnu8iNIYQ 534 - 5.7 . 14 iV27v9s4HFOrpSOJNGufv1Hg0wU5s> Please log in via your web browser and 534 - 5.7 . 14 then try again. 534 - 5.7 . 14 Learn more at 534 5.7 . 14 https: //support.google.com/mail/answer/78754 q6sm2366693pgp.58 - gsmtp |
The reason for this error was that my Gmail/G Suite email (i.e email using a custom domain) was not configured to allow sending email from less secure apps such as ours. For this, you need to visit: https://www.google.com/settings/security/lesssecureapps and enable “Allow less secure applications” toggle which looks like:
Sometimes when you visit the less secure apps link, you will see something as shown below:
In such a scenario, you might be using G Suite and you need your Administrator to enable Less secure apps feature and this can be done by following the steps:
- Navigate to http://google.com/a/<domain name>
- Navigate to Security setting from the menu as shown in the image below:
- Click on “Basic Settings” on the security settings page as shown below:
- On the Basic Settings page, look for Less Secure apps section and then click on “Go to settings for less secure apps” as shown below:
- Now on the Less secure apps page, you have the following options:
Select “Allow users to manage their access to less secure apps” and click on Save button available at the bottom of the page. This will allow individual users to control access to their email from less secure apps.
Now navigate to https://www.google.com/settings/security/lesssecureapps page and now you will be able to see the toggle for updating the “Allow less secure apps” option.
Reference: | Using Gmail as SMTP server from Java, Spring Boot apps from our JCG partner Mohamed Sanaulla at the Experiences Unlimited blog. |