JavaMail with Gmail:535-5.7.1不接受用户名和密码

当我尝试使用JavaMail API发送邮件时出现此错误。 我确信用户名和密码是100%正确的。 我正在连接的Gmail帐户是一个旧帐户,因为他们说它需要时间来处理新帐户。

 DEBUG SMTP RCVD:535-5.7.1不接受用户名和密码。 了解更多信息

 535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 x35sm3011668
 wfh.6

 javax.mail.SendFailedException:发送失败;
  嵌套exception是:
         javax.mail.AuthenticationFailedException
        在javax.mail.Transport.send0(Transport.java:218)
        在javax.mail.Transport.send(Transport.java:80)
        在Main。(Main.java:41)
        在Main.main(Main.java:51)

这是我的代码:

  import javax.mail。*;
 import javax.mail.internet。*;
 import java.util。*;

公共课主
 {
     String d_email =“abc@gmail.com”,
             d_password =“通过”,
             d_host =“smtp.gmail.com”,
             d_port =“465”,
             m_to =“abc@gmail.com”,
             m_subject =“测试”,
             m_text =“测试电子邮件。”;

    公共主要()
     {
        属性props = new Properties();
         props.put(“mail.smtp.user”,d_email);
         props.put(“mail.smtp.host”,d_host);
         props.put(“mail.smtp.port”,d_port);
         props.put( “mail.smtp.starttls.enable”, “真”);
         props.put(“mail.smtp.auth”,“true”);
         props.put(“mail.smtp.debug”,“true”);
         props.put(“mail.smtp.socketFactory.port”,d_port);
         props.put(“mail.smtp.socketFactory.class”,“javax.net.ssl.SSLSocketFactory”);
         props.put(“mail.smtp.socketFactory.fallback”,“false”);

         SecurityManager security = System.getSecurityManager();

        尝试
         {
             Authenticator auth = new SMTPAuthenticator();
             Session session = Session.getInstance(props,auth);
             session.setDebug(真);
             MimeMessage msg = new MimeMessage(session);
             msg.setText(m_text);
             msg.setSubject(m_subject);
             msg.setFrom(new InternetAddress(d_email));
             msg.addRecipient(Message.RecipientType.TO,new InternetAddress(m_to));
             Transport.send(MSG);
         }
         catch(例外mex)
         {
             mex.printStackTrace();
         } 
     }

     public static void main(String [] args)
     {
        主要的blah = new Main();
     }

    私有类SMTPAuthenticator扩展了javax.mail.Authenticator
     {
         public PasswordAuthentication getPasswordAuthentication()
         {
            返回新的PasswordAuthentication(d_email,d_password);
         }
     }
 }

给定的代码段在我的Gmail帐户上运行正常,因此问题出在其他地方。 您是否按照错误消息中给出的链接进行操作 ? 它包含以下提示:

  • 确保您输入了完整的电子邮件地址(例如username@gmail.com)
  • 重新输入密码以确保密码正确无误。 请记住,密码区分大小写。
  • 确保您的邮件客户端未设置为过于频繁地检查新邮件。 如果您的邮件客户端每10分钟检查一次以上的新邮件,您的客户端可能会反复请求您的用户名和密码。

特别是最后一点很重要。 谷歌对此非常严格。 如果您尝试以编程方式在一分钟内连接Gmail超过10次,那么您可能已被阻止。 有一点耐心,一段时间后它会被解锁。

如果您希望更自由地发送邮件,我建议您寻找专用邮件主机或设置自己的邮件服务器,例如Apache James或Microsoft Exchange。 我已经在你之前的一个问题中详细解答了这个问题。

我遇到了完全相同的问题,对我来说原因是我在我的Gmail帐户上启用了两步validation 。

生成一个新的特定于应用程序的密码并在我的java应用程序中使用它后,这个“535 5.7.1”问题就消失了。

您可以在此官方Google指南后生成新的应用程序专用密码。

我有相同的错误消息,这就是我如何解决这个问题,

创建应用密码:以下是我们如何生成应用密码,

1. Visit your App passwords page. You may be asked to sign in to your Google Account. 2. At the bottom, click Select app and choose the app you're using. Click Select device and choose the device you're using. 3. Select Generate. 4. Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device. 5. Select Done. 

我为一个Spring启动应用程序工作,我得到了应用程序密码,例如, sadsadaffferere的电子邮件地址xyz@gmail.com 。 所以,我需要配置如下的应用程序属性,

 # the email settings # ------------------ spring.mail.host=smtp.gmail.com spring.mail.username=xyz@gmail.com spring.mail.password=sadsadaffferere spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.socketFactory.port=465 spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory spring.mail.properties.mail.smtp.socketFactory.fallback=false support.email=xyz@gmail.com 

事后一切都很好