Stephan
Stephan

Reputation: 39

Java Mail - Email Not Sending through Gmail

I am trying to execute the following set of code but it's to no avail. I am using my Gmail account for this. I have 2-step verification disabled and I have allow less secured apps turned on yet I keep getting the error:

javax.mail.AuthenticationFailedException: 534-5.7.14

public void sendEmail()
{
    try
    {
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props, new javax.mail.Authenticator()
        {
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication("[email protected]", "password");
            }
        });

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
        message.setSubject("Testing Subject");
        message.setText("Testing Text");

        Transport.send(message);

        System.out.println("Mail Sent!");

    } 

    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}

Upvotes: 2

Views: 1951

Answers (1)

Stephan
Stephan

Reputation: 39

All I needed to do was enable 2-step verification then generate an app password for the application. Thanks to everyone who responded. :)

Upvotes: 2

Related Questions