Using Gmail as SMTP server from Java, Spring Boot apps

Gmail users can use Gmail’s SMTP server to send emails from their Spring Boot apps. For this let us do some setup in the app:

  1. Provide SMTP connection properties in the file:
    spring.mail.username=<your gmail/google app email>
  2. Use Spring Boot Email tools library – which is a wrapper over Spring Boot Email starter library.  Add the following in your pom.xml:
  3. Annotation your application’s main class (i.e class annotated with @SpringBootApplication) with @EnableEmailTools:
    public class EmailApplication {
        public static void main(String[] args){
  , args);
  4. Let’s write a test which uses it.ozimov.springboot.mail.service.EmailService bean to send an email:
    public class EmailServiceTest {
        it.ozimov.springboot.mail.service.EmailService emailService; 
        @Value("${spring.mail.username}") String fromEmail; 
        public void testSendEmail() throws UnsupportedEncodingException { 
            User user = new User(); 
            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 ...")

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:

Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 
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 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: 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:

  1. Navigate to<domain name>
  2. Navigate to Security setting from the menu as shown in the image below:3.PNG
  3. Click on “Basic Settings” on the security settings page as shown below:4.PNG
  4. 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:5.PNG
  5. Now on the Less secure apps page, you have the following options:6
    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 page and now you will be able to see the toggle for updating the “Allow less secure apps” option.

2 thoughts on “Using Gmail as SMTP server from Java, Spring Boot apps”

Leave a Reply