无法通过gmail smtp发送消息

时间:2015-12-16 07:27:03

标签: java email gmail

我有以下代码。

  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("myMail@gmail.com"));
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse("myMail@gmail.com"));
                message.setSubject("subject");
                message.setText(s);
                Transport.send(message);
            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
        }

密码和用户名有效。但例外是我唯一得到的:

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

我的accaunt中未启用两步验证。怎么办?

1 个答案:

答案 0 :(得分:2)

您可以在Gmail中启用“安全性较低的应用”功能,以便您的代码能够运行。您可以通过Google console

访问该内容

除此之外,您可能希望更新代码如何在here中为java访问gmail API。