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

Python实现Gmail SMTP认证报错:Error 535 用户名密码不被接受

Gmail SMTP 535 认证失败问题解决

运行用于Gmail SMTP认证的Python脚本test_smtp.py时,收到认证失败错误:

Error: (535, b'5.7.8 Username and Password not accepted. For more information, go to\n5.7.8 https://support.google.com/mail/?p=BadCredentials w14-20020adfee4e000000b0033609750752sm6314914wro.8 - gsmtp')

脚本与配置文件内容

test_smtp.py代码:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from configparser import ConfigParser

def send_test_email():
    config = ConfigParser()
    config.read("config.ini")

    sender_email = config.get("Email", "sender_email")
    receiver_email = config.get("Email", "receiver_email")
    password = config.get("Email", "password")

    message = MIMEMultipart("alternative")
    message["Subject"] = "Test Email"
    message["From"] = sender_email
    message["To"] = receiver_email

    text = "Just a test email."
    part1 = MIMEText(text, "plain")
    message.attach(part1)

    try:
        with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
            server.login(sender_email, password)
            server.sendmail(sender_email, receiver_email, message.as_string())
        print("Email sent successfully.")
    except Exception as e:
        print("Error sending email: ", e)

if __name__ == "__main__":
    send_test_email()

config.ini配置:

[Email]
sender_email = favmusic31@gmail.com
receiver_email = acccountff@gmail.com
password = passwword :(

解决步骤

  • 替换为应用密码:当前配置里的passwword :(是无效测试密码,且Gmail已停止支持直接用账户密码登录SMTP。需先给Google账户开启两步验证,然后生成16位的应用密码,将其填入config.ini的password字段。
  • 解除账户安全限制:登录Google账户后台,确认账户未被锁定、未触发安全拦截(如异地登录提示),若有相关限制,先完成验证解除。
  • 确认SMTP参数有效性:脚本中smtp.gmail.com+465端口的SSL连接配置是正确的,无需修改;若改用TLS连接,可切换至587端口并在登录前调用starttls()方法,但当前配置可正常使用。

内容的提问来源于stack exchange,提问作者Kernel-rb

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 14:34:58