Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

AuthenticationFailedException using Gmail in Javax

I’m developing a method to send emails, but Gmail doesn’t work. I’m reciving:

javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted.

The idea of the code is to send a simple message:

class SimpleAuth extends Authenticator {
    public String username = null;
    public String password = null;

    public SimpleAuth(String user, String pwd) {
        username = user;
        password = pwd;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
}

public boolean send() throws Exception {
    // Smtp server
    final String smtp = "smtp.gmail.com";
    final String port = "465";

    // Properties setting
    Properties props = new Properties();

    props.setProperty("mail.host", "cServSmtp");
    props.put("mail.transport.protocol", "smtp");

    props.put("mail.smtp.host", smtp);
    props.put("mail.smtp.auth", "true");

    props.put("mail.smtp.user", username);
    props.put("mail.debug", "true");
    props.put("mail.smtp.port", port);
    props.put("mail.smtp.socketFactory.port", port);

    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

    props.put("mail.smtp.socketFactory.fallback", "true");

    props.put("mail.smtp.ssl.protocols", "TLSv1.2");

    props.put("mail.smtp.starttls.enable", "true");

    // Authentication
    SimpleAuth auth = new SimpleAuth(username, password);

    // Instance session
    Session session = Session.getInstance(props, auth);
    session.setDebug(false);

    // Message setting
    Message msg = new MimeMessage(session);
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail));

    msg.setFrom(new InternetAddress(username, "Diego Borba"));
    msg.setSubject("Subject");
    msg.setContent("Text here", "text/plain");

    // Instance transport
    Transport tr = session.getTransport();

    try {
        // Send e-mail
        tr = session.getTransport("smtp");
        tr.connect(smtp, username, password);
        msg.saveChanges();
        tr.sendMessage(msg, msg.getAllRecipients());
        tr.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

I changed a bit of the code to facilitate reproduction, but the idea is the same.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Important observations:

  • It worked with Outlook and Hotmail (smtp.office365.com)
  • I made sure the username and password are correct.
  • I must use SSL.
  • I checked Google Support, but I didn’t find anything.
  • I’m using Java8 (Jre 1.8.0_361)

>Solution :

This happends becouse you dont alowed the Less Secure Apps in your Google account.

You just need to change this configuration!

Check this out: Solve error javax.mail.AuthenticationFailedException

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading