我正在尝试执行以下代码集,但没有成功。我正在使用我的 Gmail 帐户。我禁用了两步验证,并且允许打开不太安全的应用程序,但我仍然收到错误:
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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="34514c5559445851745359555d581a575b59" rel="noreferrer noopener nofollow">[email protected]</a>", "password");
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1b4a9b0bca1bdb491b6bcb0b8bdffb2bebc" rel="noreferrer noopener nofollow">[email protected]</a>"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e1b061f130e121b3e1d11130e1f1007501d1113" rel="noreferrer noopener nofollow">[email protected]</a>"));
message.setSubject("Testing Subject");
message.setText("Testing Text");
Transport.send(message);
System.out.println("Mail Sent!");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
最佳答案
我所需要做的就是启用两步验证,然后为应用程序生成应用程序密码。感谢大家的回应。 :)
关于Java Mail - 电子邮件不通过 Gmail 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46006443/