Search code examples
javaemailgmail

Can not send message via gmail smtp


I've got the following code.

  public static void main(String[] args)
        {
            sendMessage("hello");
        }

        private static void sendMessage(String s) {

            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(username, password);
                }
            });

            try {               
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("[email protected]"));
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse("[email protected]"));
                message.setSubject("subject");
                message.setText(s);
                Transport.send(message);
            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
        }

Password and user name are valid. But exception is the only thing I've got:

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtN2
534-5.7.14 -5TkN4GaweC6hf3rGNq9_jNkn3li8xHQJsvsrBtvsgYtTItKuGxzOh0JxrqqrtlGEKUfRj
534-5.7.14 ObHLIRLXI877ZwP120TCNTgXfCCj-sxKvs4LMuwQTm-MlRVj43NtxpqZAWM7MtzxwCde0w
534-5.7.14 -NrEnlwJVoqdYPvJjsbZyzN4wL-nETdGCAkC7wSOv_56NCEMh4riOM_M5AlR4Uf0FnsZB3
534-5.7.14 PdfTBLfznxA9Q6yesAh5ib8uebGU> 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 dz6sm720370lbb.17 - gsmtp

    at ru.tenet.es09.runnable.SendMailRunnable.sendMessage(SendMailRunnable.java:62)
    at ru.tenet.es09.runnable.SendMailRunnable.main(SendMailRunnable.java:32)
Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtN2
534-5.7.14 -5TkN4GaweC6hf3rGNq9_jNkn3li8xHQJsvsrBtvsgYtTItKuGxzOh0JxrqqrtlGEKUfRj
534-5.7.14 ObHLIRLXI877ZwP120TCNTgXfCCj-sxKvs4LMuwQTm-MlRVj43NtxpqZAWM7MtzxwCde0w
534-5.7.14 -NrEnlwJVoqdYPvJjsbZyzN4wL-nETdGCAkC7wSOv_56NCEMh4riOM_M5AlR4Uf0FnsZB3
534-5.7.14 PdfTBLfznxA9Q6yesAh5ib8uebGU> 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 dz6sm720370lbb.17 - 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 ru.tenet.es09.runnable.SendMailRunnable.sendMessage(SendMailRunnable.java:60)
    ... 1 more

Two steps authentication is not enabled in my accaunt. What do?


Solution

  • You can turn on the "less secure apps" feature in gmail so that your code will be able to run. you can access that from your google console

    Other than that, you might want to update your code how to access the gmail API nowadays for java in here.