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

如何通过SMTP服务器自动认证发邮件?遇Gmail 535错误求助

Gmail SMTP 535认证错误解决方案(Spring Mail场景)

测试Spring Mail发送Gmail时持续触发以下错误:

Username and Password not accepted. Learn more at 535 5.7.8
https://support.google.com/mail/?p=BadCredentials
h25-20020a170906855900b00993017b64a9sm7314529ejy.223 - gsmtp

已确认用户名密码正确,且谷歌已移除“允许不安全应用”选项,现有配置及代码如下:

application.yml:

mail:
    host: smtp.gmail.com
    port: 587
    username: emailtestingtool1.0@gmail.com
    password: ****
    properties:
        mail:
            smtp:
                auth: true
                starttls:
                    enable: true

sendEmail方法:

private void sendEmail(String email, String resetPasswordLink) throws MessagingException, UnsupportedEncodingException {

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);

        helper.setFrom("emailtestingtool1.0@gmail.com", "SBI intern");
        helper.setTo(email);

        String subject = "Here's the link to reset your password";

        String content = "<p>Hello, </p>"
                + "<p>You have requested to reset ur password.</p>"
                + "<p>Click the link below:</p>"
                + "<p><b><a href= \"" + resetPasswordLink + "\">Change my password</a></b></p>"
                + "<p>Ignore this email if u have not made the request.</p>";

        helper.setSubject(subject);
        helper.setText(content, true);

        mailSender.send(message);
    }

具体解决步骤

  • 启用两步验证并生成应用专用密码
    这是谷歌目前唯一支持的非OAuth认证方式:

    1. 登录目标谷歌账号,进入账号安全设置
    2. 开启两步验证(未开启则先完成此步骤)
    3. 在两步验证页面找到“应用专用密码”选项,生成一个新密码(命名可设为“Spring Mail”)
    4. 将application.yml中的password替换为这个生成的专用密码,原账号密码不再用于SMTP认证
  • 补充SMTP配置细节
    为避免连接超时或兼容性问题,建议添加超时配置:

    mail:
        host: smtp.gmail.com
        port: 587
        username: emailtestingtool1.0@gmail.com
        password: 生成的应用专用密码
        properties:
            mail:
                smtp:
                    auth: true
                    starttls:
                        enable: true
                    timeout: 5000
                    connectiontimeout: 5000
                    writetimeout: 5000
    
  • 确认发件人一致性
    确保helper.setFrom使用的邮箱与配置中的username完全一致(当前代码已符合要求,后续调整需注意)

  • 排查账号安全状态
    登录Gmail账号,检查是否有未处理的安全提醒(如陌生设备登录授权请求),手动确认后再测试发送

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 07:03:22