gpt4 book ai didi

java - 无法通过 gmail smtp 发送消息

转载 作者:行者123 更新时间:2023-12-02 04:05:38 24 4
gpt4 key购买 nike

我有以下代码。

  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)是我唯一拥有的:

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

我的帐户中未启用两步身份验证。做什么?

最佳答案

您可以在 Gmail 中打开“不太安全的应用程序”功能,以便您的代码能够运行。你可以从你的谷歌访问它 console

除此之外,您可能需要更新您的代码,以了解当今 here 中的 java 访问 gmail API 的方法。 。

关于java - 无法通过 gmail smtp 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34306178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com