PSP Logo

5.4 Annex I - GMail configurations

IES Doctor Balmis Logo

PSP class notes by Vicente Martínez is licensed under CC BY-NC-SA 4.0

5.4 Annex I - GMail configurations

5.4.1 Common Exceptions when using Gmail SMTP

While sending an email with any of the above methods, you might get the following exceptions, even your Gmail credentials are correct.

Google security

It's important to check your account security. Change the following settings only if you are absolutely sure of what you are doing.

Do not share any of the password because your account can be used without your permission.

5.4.1.1 Two-way authentication

Error while trying to send mail: 534-5.7.9 Application-specific password required. Learn more at
534 5.7.9  https://support.google.com/mail/?p=InvalidSecondFactor r10-20020a05600c458a00b003d35acb0fd7sm14828087wmo.34 - gsmtp

jakarta.mail.AuthenticationFailedException: 534-5.7.9 Application-specific password required. Learn more at
534 5.7.9  https://support.google.com/mail/?p=InvalidSecondFactor r10-20020a05600c458a00b003d35acb0fd7sm14828087wmo.34 - gsmtp

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:947)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:858)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:762)
    at jakarta.mail.Service.connect(Service.java:364)
    at jakarta.mail.Service.connect(Service.java:222)
    at jakarta.mail.Service.connect(Service.java:171)
    at jakarta.mail.Transport.send0(Transport.java:230)
    at jakarta.mail.Transport.send(Transport.java:100)
    ...

The exception (jakarta.mail.AuthenticationFailedException) talks about the Application-specific password required. This is because your Gmail account is configured for 2-step verification so your direct Gmail password won’t work here due to security reasons. To rectify this, you need to follow the below steps.

  1. Sign in with your Google account
  2. Go to your Google account or just click on the link Google account security settingsOpen in new windowGoogle settings
  3. Click on Security from the left menu.
  4. Scroll a bit down to reach the “Signing in to Google” section – most probably 3rd section from the top.
  5. Here you can see 2-Step Verification is turned On. Google settings
  6. Click on App Password just below it. Google will ask you to re-enter the password.
  7. On the next screen, you need to select the App and Device.
  8. From the “Select app” dropdown, just select Other (Custom name).
  9. Give an appropriate name like “Web” and press Generate button. Google settings
  10. You will a generated password on the screen with a popup.
  11. Save and use this password for all your Java mail code.

5.4.1.2 Less secure apps

Error while trying to send mail: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials o9-20020a05600c510900b003c6f8d30e40sm15602278wms.31 - gsmtp

jakarta.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials o9-20020a05600c510900b003c6f8d30e40sm15602278wms.31 - gsmtp

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:947)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:858)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:762)
    at jakarta.mail.Service.connect(Service.java:364)
    at jakarta.mail.Service.connect(Service.java:222)
    at jakarta.mail.Service.connect(Service.java:171)
    at jakarta.mail.Transport.send0(Transport.java:230)
    at jakarta.mail.Transport.send(Transport.java:100)
    ...
  1. Sign in with your Google account
  2. Go to your Google account or just click on the link Google less secure apps settingsOpen in new window to advance until step 5. Google settings
  3. Click on Security from the left menu.
  4. Scroll a bit down to reach the “Less secure applications".
  5. You will find it disabled. Enter the section to turn it On. Google settings
  6. Save and use your account password for all your Java mail code.
Last updated:
Contributors: Vicente Martínez