You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java类连接Gmail SMTP发送邮件认证失败,账号密码可正常登录

Gmail SMTP 认证失败问题排查与解决

问题背景

创建Gmail账号unreal@gmail.com(密码q1234567),使用Java代码发送邮件时触发AuthenticationFailedException,提示用户名密码不被接受,但网页端可正常登录。

发送邮件代码

public void sendMail() {
    String d_email = "unreal@gmail.com",
            d_uname = "unreal@gmail.com",
            d_password = "q1234567",
            d_host = "smtp.gmail.com",
            d_port = "465",
            m_to = "dummy_38@hotmail.com",
            m_subject = "Indoors Readable File: ",
            m_text = "This message is from Indoor Positioning App. Required file(s) are attached.";
    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.debug", "true");
    props.put("mail.smtp.auth", "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");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });


    session.setDebug(true);

    MimeMessage msg = new MimeMessage(session);
    try {
        msg.setSubject(m_subject);
        msg.setFrom(new InternetAddress(d_email));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

        Transport transport = session.getTransport("smtps");
        transport.connect(d_host, Integer.valueOf(d_port), d_uname, d_password);
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();

    } catch (AddressException e) {
        e.printStackTrace();

    } catch (MessagingException e) {
        e.printStackTrace();

    }

}

报错日志片段

DEBUG SMTP: protocolConnect login, host=smtp.gmail.com, user=unreal@gmail.comm, password=<non-null>
DEBUG SMTP: AUTH LOGIN failed
javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted.

解决步骤

1. 修复代码变量引用错误

从日志能看到登录用户名变成了unreal@gmail.comm(多了一个m),原因是匿名Authenticator类中使用了未定义的username和password变量,替换为代码中声明的d_uname和d_password:

Session session = Session.getInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(d_uname, d_password);
            }
        });

2. 启用应用专用密码(核心解决方法)

Gmail已禁用普通密码的SMTP登录,即使网页端能登录也无法直接使用账号密码发邮件:

  • 先为账号开启两步验证(必须前置操作)
  • 进入Google账号管理页面,生成应用专用密码
  • 将代码中的d_password替换为生成的应用专用密码

3. 简化SMTP配置参数

端口465为SSL端口,可简化配置,移除冗余的socketFactory相关属性:

Properties props = new Properties();
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.debug", "true");

4. 排查账号安全状态

  • 检查账号最近登录记录,确认是否有异常登录被拦截
  • 确保未开启账号的异常安全限制

内容的提问来源于stack exchange,提问作者user1999453

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 08:47:03