2010-06-03 111 views
8

当我尝试使用JavaMail API发送邮件时出现此错误。我确信用户名和密码是100%正确的。我连接的Gmail帐户是一个较旧的帐户,因为他们说需要时间才能使用新帐户。带Gmail的JavaMail:535-5.7.1不接受用户名和密码

 
DEBUG SMTP RCVD: 535-5.7.1 Username and Password not accepted. Learn more at 

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

javax.mail.SendFailedException: Sending failed; 
    nested exception is: 
     javax.mail.AuthenticationFailedException 
     at javax.mail.Transport.send0(Transport.java:218) 
     at javax.mail.Transport.send(Transport.java:80) 
     at Main.(Main.java:41) 
     at Main.main(Main.java:51) 

,这是我的代码:

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

public class Main 
{ 
    String d_email = "[email protected]", 
      d_password = "pass", 
      d_host = "smtp.gmail.com", 
      d_port = "465", 
      m_to = "[email protected]", 
      m_subject = "Testing", 
      m_text = "testing email."; 

    public Main() 
    { 
     Properties 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","true"); 
     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(); 

     try 
     { 
      Authenticator auth = new SMTPAuthenticator(); 
      Session session = Session.getInstance(props, auth); 
      session.setDebug(true); 
      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 (Exception mex) 
     { 
      mex.printStackTrace(); 
     } 
    } 

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

    private class SMTPAuthenticator extends javax.mail.Authenticator 
    { 
     public PasswordAuthentication getPasswordAuthentication() 
     { 
      return new PasswordAuthentication(d_email, d_password); 
     } 
    } 
} 
+4

@bobby:你的密码的base64编码值在原来的职位是可见的。我在一小时前删除了它,但它在编辑历史记录中仍然可见。我强烈建议您在恶意黑客破坏您的Gmail帐户之前更改您的Gmail密码。 – BalusC 2010-06-03 12:06:34

回答

5

给定的代码段工作正常,在我的Gmail帐户,所以这个问题就出在别处。你关注了the link given in the error message吗?它包含以下提示:

  • 确保您输入完整的电子邮件地址(例如[email protected]
  • 重新输入密码,以确保它是正确的。请记住,密码区分大小写。
  • 确保您的邮件客户端未设置为经常检查新邮件。如果您的邮件客户端每10分钟检查一次以上的新邮件,您的客户端可能会重复请求您的用户名和密码。

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

如果您希望发送邮件更自由,我建议您寻找专用邮件主机或设置自己的邮件服务器,例如Apache James或Microsoft Exchange。在您之前的问题中,我已经有answered this in detail

+0

hhey谢谢...它工作.. !!你能告诉我如何使用jsp发送邮件.. !!即时通讯试图在网站上实现'忘记密码'是否有任何想法和建议从你来实现这个..! – simplyblue 2010-06-03 12:03:24

+0

只需创建一个servlet类来完成任务并在JSP中创建一个HTML表单,该表单将提交给此Servlet正在侦听的URL。在servlet方法中,您可以使用通常的方式编写和调用真正的Java代码。在这里了解更多关于JSP/Servlet的信息:http://courses.coreservlets.com/Course-Materials/csajsp2.html – BalusC 2010-06-03 12:15:04

2

我遇到了完全相同的问题,对我来说,原因是我已打开我的gmail帐户2-step verification

在生成新的应用程序专用密码并在我的Java应用程序中使用该密码后,此“535 5.7.1”问题消失了。

您可以在此official google guide之后生成新的应用程序专用密码。

+0

谢谢! – Davidea 2017-09-08 13:47:14

0

我有同样的错误消息,这是我已经解决了这个问题,

创建一个应用程序密码:这里是我们如何能够生成一个应用密码,

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. 

我工作了春季启动应用程序,我得到的应用程序密码说,sadsadaffferere的电子邮件地址,​​。所以,我需要配置的应用属性,如下面

# the email settings 
# ------------------ 
spring.mail.host=smtp.gmail.com 
[email protected] 
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 
[email protected] 

一切正常之后

相关问题