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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2965251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:58:54  来源:igfitidea点击:

JavaMail with Gmail: 535-5.7.1 Username and Password not accepted

javajavamail

提问by simplyblue

I get this error when I try to send a mail using JavaMail API. I am sure that the username and password are 100% correct. The Gmail account which I'm connecting is an older account, because they say it takes time for it to work with new accounts.

当我尝试使用 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)

and this is my code:

这是我的代码:

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

采纳答案by BalusC

The given code snippet works fine on my Gmail account, so this problem lies somewhere else. Did you follow the link given in the error message? It contains the following hints:

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

  • Make sure that you've entered your full email address (e.g. [email protected])
  • Re-enter your password to ensure that it's correct. Keep in mind that passwords are case-sensitive.
  • Make sure your mail client isn't set to check for new mail too often. If your mail client checks for new messages more than once every 10 minutes, your client might repeatedly request your username and password.
  • 确保您输入了完整的电子邮件地址(例如 [email protected]
  • 重新输入您的密码以确保其正确无误。请记住,密码区分大小写。
  • 确保您的邮件客户端没有设置为过于频繁地检查新邮件。如果您的邮件客户端每 10 分钟检查一次以上的新邮件,您的客户端可能会反复请求您的用户名和密码。

Especially the last point is important. Google is very strict in this. If you're trying to connect Gmail for example more than 10 times in a minute programmatically, then you may already get blocked. Have a bit of patience, after some time it will get unblocked.

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

If you'd like more freedom in sending mails, I recommend to look for a dedicated mail host or to setup your own mail server, such as Apache James or Microsoft Exchange. I've already answered this in detailin one of your previous questions.

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

回答by Jifeng Zhang

I encountered the exact same problem, for me the reason is I have turned on 2-step verificationon my gmail account.

我遇到了完全相同的问题,对我来说,原因是我在我的 gmail 帐户上打开了两步验证

After generating a new application-specific password and use that in my java application, this "535 5.7.1" issue is gone.

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

You can generate a new application-specific password following this official google guide.

您可以按照此官方 google 指南生成新的特定于应用程序的密码。

回答by Chaklader Asfak Arefe

I have the same error message and this is how I have solved the issue,

我有相同的错误消息,这就是我解决问题的方法,

Create an app password: here is how we can generate an app password,

创建应用密码:这是我们生成应用密码的方法,

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.

I worked for a Spring boot app and I get the app password say, sadsadafffererefor the email address, [email protected]. So, I need to configure the app properties like the following,

我为 Spring boot 应用程序工作,我得到了应用程序密码,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]

Everything works fine afterwards

之后一切正常

回答by Laxman G

I had same issue : I refer this link, I have followed below steps it worked for me.

我有同样的问题:我参考了这个链接,我遵循了以下对我有用的步骤。

By default Gmail account is highly secured. When we use gmail smtp from non gmail tool, email is blocked. To test in our local environment, make your gmail account less secure as

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

  1. Login to Gmail.
  2. Access the URL as https://www.google.com/settings/security/lesssecureapps
  3. Select "Turn on"
  1. 登录 Gmail。
  2. https://www.google.com/settings/security/lesssecureapps访问 URL
  3. 选择“开启”

回答by Ashwath

I faced the same problem although my username and password were correct, but after some research, I was able to send email through applications by enabling the Less secure app accessoption on my Gmail account. You can find this feature under securityoption in the left menu.

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

Related links:

相关链接:

回答by Gaurav Dalal

If the same credential was working earlier and it stopped working then the primary reason for this problem is password mismatch/changed on gmail client and not updated in Jenkins or other CI server. If that is not the case then check for reasons mentioned by @BalusC

如果相同的凭据更早地工作并且停止工作,那么此问题的主要原因是 gmail 客户端上的密码不匹配/更改,并且未在 Jenkins 或其他 CI 服务器中更新。如果情况并非如此,请检查@BalusC 提到的原因