Yandex SMTP认证失败求助:正常运行的Python脚本突然无法登录
Yandex邮箱SMTP认证失败排查方案
我有一套用Python写的脚本,用来生成报表并从Yandex邮箱发送到企业Outlook邮箱,之前正常运行了好几年,突然在一天前失效了。我已经检查并重新配置了所有相关设置——包括创建新的应用密码、勾选IMAP权限,但问题还是没解决。
测试代码如下:
import smtplib SMTP_SERVER = "smtp.yandex.com" SMTP_PORT = 465 SMTP_USER = "mymail@yandex.kz" SMTP_PASSWORD = "xxxxxxxxxxxxxxxx" # App password (16 chars) try: with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT, timeout=20) as server: server.set_debuglevel(1) server.login(SMTP_USER, SMTP_PASSWORD) print("Success! Authentication works.") except Exception as e: print("Error:", e)
执行后返回错误信息:
535, b'5.7.8 错误:认证失败:无效的用户名或密码!
排查建议
- 核对应用密码:Yandex应用密码是16位字符,检查复制时是否多了空格、漏了字符,建议重新生成一次并直接复制粘贴到代码中。
- 切换SMTP端口测试:尝试用端口587搭配STARTTLS方式验证,替换代码如下:
import smtplib SMTP_SERVER = "smtp.yandex.com" SMTP_PORT = 587 SMTP_USER = "mymail@yandex.kz" SMTP_PASSWORD = "xxxxxxxxxxxxxxxx" try: with smtplib.SMTP(SMTP_SERVER, SMTP_PORT, timeout=20) as server: server.set_debuglevel(1) server.starttls() server.login(SMTP_USER, SMTP_PASSWORD) print("Success! Authentication works.") except Exception as e: print("Error:", e)
- 检查账号安全状态:确认Yandex账号是否被临时锁定,两步验证是否正常启用(应用密码必须在两步验证开启状态下才能生效)。
- 验证网络环境:如果脚本在新网络下运行,登录Yandex网页版完成设备验证,解除陌生IP的登录限制。
- 确认邮箱地址:核对
SMTP_USER中的邮箱后缀是否正确(确保是yandex.kz而非yandex.com)。
内容的提问来源于stack exchange,提问作者Aaron Ramsey
相关产品推荐
相关产品推荐

