Java 为什么我的电子邮件会话方法停止工作?
Java Why did my email session method stop working?
我正在使用我的一个旧项目中的 class/method 以编程方式发送电子邮件。它在当时有效,但现在我遇到了一些错误,这些错误可以避免某种 gmail 安全更新,因为我上次在旧的 project:I 中使用这个 class 尝试通过浏览器登录和注销,但是没有帮助。
堆栈跟踪显示:
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs6
534-5.7.14 ZJAPOThluIgJLcM06Bo2G599ldMO1V8IpReZebE8FvDK3uj4KCgtg7CBEUzfjv1ZRjKTY8
534-5.7.14 TnV7OuzgZSBLFq4N89fwfDcYSs1fnsyWLglDAEWbfbltw9eN5aXc0_S_7g8kw0EhLs7IDO
534-5.7.14 mEsQ-FBS3EBd_F78Qv3iOpGeSACqreH_42BZ8XnouZBwfNp02PqLVqcv7wBF_-0YdtXLx4
534-5.7.14 DZb0An9hZCoogawn1j79heQGK4g9w> 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 s59sm4398674qtd.20 - gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:893)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:814)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:728)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at javax.mail.Service.connect(Service.java:265)
at javax.mail.Transport.send0(Transport.java:251)
at javax.mail.Transport.send(Transport.java:174)
at Emailer.sendEmail(NotificationHandler.java:70)`
.
.
.
这是我的代码
class Emailer {
private static String host = "smtp.gmail.com";
private static String user = "myaccount@gmail.com";
private static String pass = "mypassword";
public static void sendEmail(String fromAddr, String toAddr, String subject, String body) {
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, null);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(fromAddr));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddr));
message.setSubject(subject);
message.setText(body);
Transport.send(message, user, pass);
}
catch (AddressException e) {
e.printStackTrace();
}
catch (MessagingException e) {
e.printStackTrace();
}
}
可能是因为 google 阻止了您的电子邮件客户端。
Google may block sign-in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safe.
我 运行 大约两个月前遇到这个问题,我的 Java 应用程序无法使用测试 Gmail 帐户发送电子邮件。这个link解释的不多。但它确实提供了禁用此功能的步骤。
我正在使用我的一个旧项目中的 class/method 以编程方式发送电子邮件。它在当时有效,但现在我遇到了一些错误,这些错误可以避免某种 gmail 安全更新,因为我上次在旧的 project:I 中使用这个 class 尝试通过浏览器登录和注销,但是没有帮助。
堆栈跟踪显示:
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs6
534-5.7.14 ZJAPOThluIgJLcM06Bo2G599ldMO1V8IpReZebE8FvDK3uj4KCgtg7CBEUzfjv1ZRjKTY8
534-5.7.14 TnV7OuzgZSBLFq4N89fwfDcYSs1fnsyWLglDAEWbfbltw9eN5aXc0_S_7g8kw0EhLs7IDO
534-5.7.14 mEsQ-FBS3EBd_F78Qv3iOpGeSACqreH_42BZ8XnouZBwfNp02PqLVqcv7wBF_-0YdtXLx4
534-5.7.14 DZb0An9hZCoogawn1j79heQGK4g9w> 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 s59sm4398674qtd.20 - gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:893)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:814)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:728)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at javax.mail.Service.connect(Service.java:265)
at javax.mail.Transport.send0(Transport.java:251)
at javax.mail.Transport.send(Transport.java:174)
at Emailer.sendEmail(NotificationHandler.java:70)`
.
.
.
这是我的代码
class Emailer {
private static String host = "smtp.gmail.com";
private static String user = "myaccount@gmail.com";
private static String pass = "mypassword";
public static void sendEmail(String fromAddr, String toAddr, String subject, String body) {
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, null);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(fromAddr));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddr));
message.setSubject(subject);
message.setText(body);
Transport.send(message, user, pass);
}
catch (AddressException e) {
e.printStackTrace();
}
catch (MessagingException e) {
e.printStackTrace();
}
}
可能是因为 google 阻止了您的电子邮件客户端。
Google may block sign-in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safe.
我 运行 大约两个月前遇到这个问题,我的 Java 应用程序无法使用测试 Gmail 帐户发送电子邮件。这个link解释的不多。但它确实提供了禁用此功能的步骤。