Java 无法通过gmail smtp发送邮件
我有以下代码Java 无法通过gmail smtp发送邮件,java,email,gmail,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.aut
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
线程“main”java.lang.RuntimeException中的异常:javax.mail.AuthenticationFailedException:534-5.7.14请通过web浏览器登录并
534-5.7.14然后重试。
534-5.7.14了解更多信息,请访问
534 5.7.14 https://support.google.com/mail/answer/78754 dz6sm720370lbb.17-gsmtp
位于ru.tenet.es09.runnable.SendMailRunnable.sendMessage(SendMailRunnable.java:62)
位于ru.tenet.es09.runnable.SendMailRunnable.main(SendMailRunnable.java:32)
原因:javax.mail.AuthenticationFailedException:534-5.7.14请通过web浏览器登录并
534-5.7.14然后重试。
534-5.7.14了解更多信息,请访问
534 5.7.14 https://support.google.com/mail/answer/78754 dz6sm720370lbb.17-gsmtp
在com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)上
在com.sun.mail.smtp.SMTPTransport.authenticate上(SMTPTransport.java:752)
在com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)上
在javax.mail.Service.connect(Service.java:317)
在javax.mail.Service.connect(Service.java:176)
在javax.mail.Service.connect(Service.java:125)
在javax.mail.Transport.send0(Transport.java:194)
在javax.mail.Transport.send(Transport.java:124)
在ru.tenet.es09.runnable.SendMailRunnable.sendMessage(SendMailRunnable.java:60)
... 还有一个
“我的帐户”中未启用两步身份验证。怎么办?你可以打开gmail中的“不太安全的应用”功能,这样你的代码就可以运行了。你可以从谷歌上访问它
除此之外,您可能还需要更新代码,以便在中访问当前用于java的gmail API