我正在尝试执行以下代码集,但它无济于事。我正在使用我的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("example@gmail.com", "password");
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("example@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("example@company.com"));
message.setSubject("Testing Subject");
message.setText("Testing Text");
Transport.send(message);
System.out.println("Mail Sent!");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
答案 0 :(得分:1)
我需要做的就是启用两步验证,然后为应用程序生成应用密码。感谢大家的回应。 :)