6

I have made a little program that send a message to my email account. Most of my code seems to work fine. However, I have encounter the AuthenticationFailedException when I am my code hit transport.send in my program. I am not sure why because I think I have everything set up correctly. Here is my code.

JavaEmail.java

public static void main (String[] args) throws Exception{
                //intialize logger 
protected static Logger logger = LogManager.getLogger(JavaEmail.class.getName());

//smtp related parameters 
private static String smtpUseremail;
private static String smtpReceiverEmailAddress;
private static String smtpUserpassword;
private static String smtpPortnumber;
private static String smtpHost;

private static String emailSubject;

//stack error message variable 
private static String message;

        try {

            logger.info("-----------------------------");
            logger.info("---------Starting up---------");
            logger.info("-----------------------------");

            initialize();

            String body = "This is a test";

            logger.info("Start to connect to email server");

            sendFromGmail(smtpUseremail, smtpUserpassword, smtpReceiverEmailAddress, emailSubject, body);



           // System.out.println("error message: " + message);

            logger.info("Email Sent");


        }
        catch (Exception e){
            logger.error(e);

            // message = e.toString();

            throw e;


        }
    }

    //Setting up Email Server
    private static void sendFromGmail(String from, String pass, String to, String subject, String body) {


        Properties props = PropertiesUtil.loadProperties("main.properties");

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", smtpPortnumber);

        Session session = Session.getInstance(props,
                  new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(smtpUseremail, smtpUserpassword);
                    }
                  });

        MimeMessage message = new MimeMessage(session);



        try{


            message.setFrom(new InternetAddress(from));

            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));


            message.setSubject(subject);

            message.setText(body);

            Transport.send(message);

            logger.info("Message has been delievered, check your mail");

        }

        catch (MessagingException e) {
            logger.error("Messaging Exception");

            throw new RuntimeException(e);
        }
    }


    //Initializing Properties
    private static void initialize() throws Exception  {

        Properties props = PropertiesUtil.loadProperties("main.properties");

        //smtp set up
        smtpPortnumber = props.getProperty("portNumber");
        smtpReceiverEmailAddress = props.getProperty("recipientEmailAddress");
        smtpUseremail = props.getProperty("userEmailAddress");
        smtpUserpassword = props.getProperty("userPassword");
        smtpHost = props.getProperty("emailHost");
        emailSubject = props.getProperty("");

    }

}

main.properties

portNumber = 465
recipientEmailAddress = [email protected]
userPassword = secert
userEmailAddress = [email protected]
emailHost = smtp.gmail.com

error

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvWE
    534-5.7.14 HIW4hBxefM-JI-wxqEK6ru2lJstC10IedXvhU_Tzbft1IqPFsot0wfjAWkrX2-gLIEG0NZ
    534-5.7.14 V45KWaeagzWTb_v1nAe-BOZSR6YzPHQ7-cYVGYplvcaexmFziL3IMRgkCalmn-5FjreRkm
    534-5.7.14 PjtRPd9nmjcosX0ce4DXSGn_y-qGOsW-ehOp2U3eQkSReObXi-a80ywNWiy3QuiV56f61h
    534-5.7.14 yz8bmLpugqIYhh7DiNd6HpwvNHAo> 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 ob15sm12759947pdb.52 - gsmtp

        at emailNotification.JavaEmail.sendFromGmail(JavaEmail.java:140)
        at emailNotification.JavaEmail.main(JavaEmail.java:63)
    Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvWE
    534-5.7.14 HIW4hBxefM-JI-wxqEK6ru2lJstC10IedXvhU_Tzbft1IqPFsot0wfjAWkrX2-gLIEG0NZ
    534-5.7.14 V45KWaeagzWTb_v1nAe-BOZSR6YzPHQ7-cYVGYplvcaexmFziL3IMRgkCalmn-5FjreRkm
    534-5.7.14 PjtRPd9nmjcosX0ce4DXSGn_y-qGOsW-ehOp2U3eQkSReObXi-a80ywNWiy3QuiV56f61h
    534-5.7.14 yz8bmLpugqIYhh7DiNd6HpwvNHAo> 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 ob15sm12759947pdb.52 - gsmtp

        at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)
        at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
        at javax.mail.Service.connect(Service.java:317)
        at javax.mail.Service.connect(Service.java:176)
        at javax.mail.Service.connect(Service.java:125)
        at javax.mail.Transport.send0(Transport.java:194)
        at javax.mail.Transport.send(Transport.java:124)
        at emailNotification.JavaEmail.sendFromGmail(JavaEmail.java:130)
        ... 1 more
3
  • Please check gmail id/password is right or not. Commented Aug 31, 2015 at 4:16
  • Hello, thank you for your respond. It is correct, this is my login gmail ac and pw Commented Aug 31, 2015 at 4:16
  • Please check the answer. Commented Aug 31, 2015 at 4:26

2 Answers 2

21

Go to browser then Login to your gmail account which you trying in your code, then go to the this link https://www.google.com/settings/security/lesssecureapps

You will see

enter image description here

You should click TURN ON .

Sign up to request clarification or add additional context in comments.

6 Comments

I turned it on but I am still getting the errors for weeks.
I have enabled this, still not working dont know whats wrong.. can you help. Its working perfectly on localhost
@viveksinghggits Contradiction in your comment, its working or not ?
@SubodhJoshi no its not wrking yet. Its working on localhost but not on production
@SubodhJoshi what do you mean by 'both places'...?
|
2

I solved this activating DisplayUnlockCaptcha :

https://accounts.google.com/b/0/DisplayUnlockCaptcha

Also activating the Lesssecureapps

https://www.google.com/settings/security/lesssecureapps

2 Comments

A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.
Activating displayUnlockCaptcha has done it for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.