JavaMail Exception javax.mail.AuthenticationFailedException 534-5.7.9 Application-specific password required
Asked Answered
W

7

25

I want to send mail using JavaMailAPI

I have done some coding but it is not working throwing Exception:-

Message Sending Failedjavax.mail.AuthenticationFailedException: 534-5.7.9 Application-specific password required.

package com.appreciationcard.service;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.appreciationcard.dao.DAOFactory;
import com.appreciationcard.forms.ComposeForm;

public class MailServiceImpl implements MailService {

public boolean mailsent(ComposeForm composeForm) {
    String to = composeForm.getTo();
    String from = composeForm.getFrom();
    String cc = composeForm.getCc();
    String bcc = composeForm.getBcc();
    String subject = composeForm.getSubject();
    String messages = composeForm.getMessage();
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.port", "587");
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.debug", "true");
    System.out.println("Properties" + props);
    Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(
                            "[email protected]", "xxxx");
                }
            });
    try {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                to));
        message.setSubject(subject);
        message.setText(messages);
        Transport.send(message);
    } catch (MessagingException mex) {
        System.out.println("Message Sending Failed" + mex);
        mex.printStackTrace();
    } 

}

}

I am getting Exception on server console

Message Sending Failedjavax.mail.AuthenticationFailedException: 534-5.7.9 Application-specific password required.

Learn more at 534 5.7.9 http://support.google.com/accounts/bin/answer.py?answer=185833 o5sm11464195pdr.50 - gsmtp

Will Any one Kindly Help me out to solve this problem.

Woolgrower answered 27/10, 2014 at 18:13 Comment(3)
To be clear, have you followed the instructions in the link and generated an app-specific password? i.e., are you getting the error even with an app-specific password?Tasman
Did you read that page?Hochman
On some systems the link to the helpful URL listed above gets munged. That is what led me here- so this is still a helpful question in that context.Apc
D
38

You have enabled Two phase authentication for your Google account and as a result applications will not be able to login to your Google account using the actual password. Google expects you to generate a application specific password for each application you use (and give it a name) and then use that password to login to your Google account from your application. This allows you to not give your password to third party application when you have 2-step authentication enabled.

The alternate way is for your application to support redirecting to a Google page to authenticate using username and password and code generated by the Google Authenticator app.

The link clearly explains what to do.

Disaccord answered 27/10, 2014 at 18:24 Comment(2)
Thank You very much for your valuable information.My Problem Solved mail sent successfully.Thanks Mr.Saket for your quick reply.ThanksWoolgrower
Thanks! had been looking for this since last 1 hour!!Schistosomiasis
S
32

UPDATED TO 2023


  1. two steps verification - You Must this step to continue

enter image description here

2)login and add a phone number and verify with text messege

than will become after green"V" enter image description here

3)press on the "2-Step Verification" , go down to the end of the list and click on "App passwords"

enter image description here

  1. then in "Select App" write the name of the app and press "Create"

enter image description here

write a description in the open page and press genarate

  1. in the open "Genarated App Password copy the created password and use it in your smpt password as usual password(Check you dont copy spaces) that it
    enter image description here

How to use it Go to the settings for your Google Account in the application or device you are trying to set up. Replace your password with the 16-character password shown above. Just like your normal password, this app password grants complete access to your Google Account. You won't need to remember it, so don't write it down or share it with anyone.

Squint answered 12/6, 2022 at 14:5 Comment(1)
This approach works at 2022Libre
E
10

Just create an App Password for your account and use that password.

Steps to create password:

Go to your account settings (https://myaccount.google.com/) -->> Security -->> Under signing in to Google -->> App Password -->> Enter your credentials to login to your account -->> Select 'App' and 'Device' -->> Generate.

Copy and paste the password somewhere.

You can use this password instead of your account password.

Earlap answered 1/2, 2021 at 9:10 Comment(1)
I find the generated 16 digits password can be used by more than one time?Clownery
A
2

You're probably trying to send mail through a gmail account in which you've enabled 2-factor authentication. A 6-digit authentication token sent to your mobile device as an SMS message or generated through the Google Authenticator app is required to proceed. Alternatively, you can generate app-specific passwords through Google's web based ui and use that instead when accessing the mail account through your java code. The link you included in your question walks you through the process.

Amylene answered 27/10, 2014 at 18:23 Comment(0)
K
0

The error message and KB article seems to be quite correct: you need to use an application password for Google Mail server. You can create those throw-away passwords on the Account page of google.

Kaleykaleyard answered 27/10, 2014 at 18:22 Comment(0)
S
0

If you are not able to find App Password..

  1. First turn on two step authentication.
  2. Go to Link https://myaccount.google.com/apppasswords and set up the app password.
Savate answered 20/6, 2023 at 16:35 Comment(0)
C
0

Gmail Account -> Security - > 2 - Step Verification - > App passwords (Bottom of Page) - Create App Password account (Provide any name) - Save - Password will generated.

Take password and save it on Password field under "Use SMTP Authentication" section.

This is latest one and worked for me.

Ceremonial answered 12/10, 2023 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.