Java为什么我的电子邮件会话方法停止工作?

时间:2016-09-02 05:09:03

标签: java smtp gmail

我正在使用我的旧项目中的类/方法以编程方式发送电子邮件。它起作用了,但是现在我收到了错误,因为我上次在旧项目中使用了这个类,因此我在某些gmail安全更新中遇到了错误:我尝试通过浏览器登录和注销,但这没有帮助。 / p>

堆栈跟踪显示:

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();
       }
}

1 个答案:

答案 0 :(得分:0)

可能是因为谷歌阻止了您的电子邮件客户端。

  

Google可能会阻止某些不使用现代安全标准的应用或设备的登录尝试。由于这些应用和设备更容易入侵,因此阻止它们有助于确保您的帐户安全。

我在两个月前遇到了这个问题,我的Java应用程序无法使用测试Gmail帐户发送电子邮件。这个link没有详细解释。但它确实提供了禁用此功能的步骤。