Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.mail.AuthenticationFailedException Java Mail

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
like image 648
RedRocket Avatar asked Aug 31 '15 04:08

RedRocket


2 Answers

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 .

like image 97
Subodh Joshi Avatar answered Nov 04 '22 18:11

Subodh Joshi


I solved this activating DisplayUnlockCaptcha :

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

Also activating the Lesssecureapps

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

like image 34
arjun kumar Avatar answered Nov 04 '22 18:11

arjun kumar