微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javax.mail.AuthenticationFailedException: 在 Transport.send(Message) 期间连接失败

如何解决javax.mail.AuthenticationFailedException: 在 Transport.send(Message) 期间连接失败

3 个月前,我通过 gmail 发送邮件代码运行良好。但是当我今天再次检查时,它因以下错误而失败。 我的 Gmail 帐户未通过 2 因素身份验证。

代码

package com.mail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mailer {
    
    public static void sendMail(String messageSubject,String messageString)
    {
         Properties props = new Properties();    
         props.put("mail.smtp.host",ConstantsHolder.MAIL_HOST); 
         props.put("mail.smtp.user",ConstantsHolder.FROM_EMAIL);
         props.put("mail.smtp.socketFactory.port","465");    
         props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");    
         props.put("mail.smtp.auth","true");    
         props.put("mail.smtp.port","465");
         props.put("mail.smtp.debug","true");
         props.put("mail.smtp.socketFactory.fallback","false");

         //To use TLS
         props.put("mail.smtp.starttls.enable","true");
         
         //get Session   
         Session session = Session.getDefaultInstance(props,new Authenticator() {
          protected PasswordAuthentication getpasswordAuthentication() {    
          return new PasswordAuthentication(ConstantsHolder.FROM_EMAIL,ConstantsHolder.FROM_EMAIL_PASSWD);  
          }    
         });    
         //compose message    
         try {    
          MimeMessage message = new MimeMessage(session);    
          message.addRecipient(Message.RecipientType.TO,new InternetAddress(ConstantsHolder.TO_EMAIL));    
          message.setSubject(messageSubject);    
          message.setText(messageString);    
          //send message  
          Transport.send(message);
          System.out.println("message sent successfully");    
         } 
         catch (MessagingException e) 
         {
             System.out.println("Failed " + e.getMessage()); 
             throw new RuntimeException(e);
         } 
    }
}

错误是:

Failed Failed to connect
Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: Failed to connect
    at com.mail.Mailer.sendMail(Mailer.java:57)
    at com.mail.MailMain.main(MailMain.java:7)
Caused by: javax.mail.AuthenticationFailedException: Failed to connect
    at javax.mail.Service.connect(Service.java:322)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at com.mail.Mailer.sendMail(Mailer.java:51)
    ... 1 more

在这里做错了吗? 我还检查了其他问题,但它与“无法连接:未提供密码”有关

================================================ ================ 编辑:解决方 按照@Nasten1988 的要求尝试打开调试模式后,我找到了问题的根本原因并能够继续。因此将@Nasten1988 的答案标记为正确答案。

阅读我对实际问题的回答。

================================================ ==================

解决方法

无法连接:也许您的连接被防火墙阻止了,您的软件是最新的吗?未提供密码:检查 Google 的要求,他们可能会更改它们并检查您的邮件方法的密码是如何提供的。也许调试器可以帮助你。这就是我要寻找的。​​p>

,

所以这对我有帮助 -

我将邮件会话的调试模式设置为 true -

session.setDebug(true);

显示了实际问题 - 我的 Gmail 帐户中安全性较低的应用程序已关闭。 显然,如果它一段时间不使用,它会自动关闭。

https://myaccount.google.com/lesssecureapps

您必须先关闭 2-Factor 身份验证,然后才能允许安全性较低的应用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。