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

问题描述 投票:13回答:5

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

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);
        }
    }
}
java javamail
5个回答
10
投票

给定的代码段在我的Gmail帐户上运行正常,因此问题出在其他地方。你关注the link given in the error message了吗?它包含以下提示:

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

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

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


20
投票

我有同样的问题:我推荐这个link,我已经按照以下步骤为我工作了。

默认情况下,Gmail帐户具有高度安全性当我们从非gmail工具使用gmail smtp时,电子邮件被阻止。要在我们的本地环境中进行测试,请将您的Gmail帐户设置为不太安全

  1. 登录Gmail。
  2. 访问URL为https://www.google.com/settings/security/lesssecureapps
  3. 选择“开启”

7
投票

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

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

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


1
投票

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

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

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的电子邮件地址,[email protected]。所以,我需要配置如下的应用程序属性,

# 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]

事后一切都很好


1
投票

虽然我的用户名和密码是正确的,但我遇到了同样的问题,但经过一些研究,我能够通过启用Gmail帐户上的安全性较低的应用访问选项来通过应用程序发送电子邮件。您可以在左侧菜单中的security选项下找到此功能。

相关链接:

© www.soinside.com 2019 - 2024. All rights reserved.