Java 邮件 - 电子邮件未通过 Gmail 发送
Java Mail - Email Not Sending through Gmail
我正在尝试执行以下代码集,但无济于事。为此,我正在使用我的 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();
}
}
我需要做的就是启用两步验证,然后为应用程序生成一个应用程序密码。感谢大家的回应。 :)
我正在尝试执行以下代码集,但无济于事。为此,我正在使用我的 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();
}
}
我需要做的就是启用两步验证,然后为应用程序生成一个应用程序密码。感谢大家的回应。 :)