首页 文章

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

提问于
浏览
9

当我尝试使用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 = "abc@gmail.com",
            d_password = "pass",
            d_host = "smtp.gmail.com",
            d_port  = "465",
            m_to = "abc@gmail.com",
            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);
        }
    }
}

3 回答

  • 1

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

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

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

  • 6

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

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

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

    如果您在之前的一个问题中已经answered this in detail已经answered this in detail .

  • 4

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

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

    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
    

    事后一切都很好

相关问题