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

使用Yahoo SMTP发邮件遇smtplib.SMTPServerDisconnected错误求助

Yahoo SMTP登录失败问题排查与解决

我尝试用Yahoo服务器发送邮件失败,换Gmail时错误更严重。我知道问题出在SMTP.login()方法,但没法解决。以下是我的代码和报错信息:

import smtplib, ssl

sender = '******@yahoo.com'
receiver = sender
password = '******'
msg = 'just a test'  
port = 587


with smtplib.SMTP('smtp.mail.yahoo.com', port) as server:
    server.connect('smtp.mail.yahoo.com', port)
    server.set_debuglevel(1)
    server.ehlo()
    print('connected')
    server.starttls()
    server.ehlo()
    server.login(sender, password)
    print('logged in')
    server.sendmail(sender, receiver, msg)
    print('sent')

报错信息:

send: 'ehlo [192.168.1.8]\r\n'
connected
reply: b'250-hermes--canary-production-ir2-f74ffc99c-2vqg6 Hello [192.168.1.8] 
[194.44.96.40])\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-ENHANCEDSTATUSCODES\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-SIZE 41697280\r\n'
reply: b'250 STARTTLS\r\n' 
reply: retcode (250); Msg: b'hermes--canary-production-ir2-f74ffc99c-2vqg6 Hello [192.168.1.8] 
[194.44.96.40])\nPIPELINING\nENHANCEDSTATUSCODES\n8BITMIME\nSIZE 41697280\nSTARTTLS'
send: 'STARTTLS\r\n'
reply: b'220 2.0.0 Ready to start TLS\r\n'
reply: retcode (220); Msg: b'2.0.0 Ready to start TLS'
send: 'ehlo [192.168.1.8]\r\n'
connection established
reply: b'250-hermes--canary-production-ir2-f74ffc99c-2vqg6 Hello [192.168.1.8]    
[194.44.96.40])\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-ENHANCEDSTATUSCODES\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-SIZE 41697280\r\n'
reply: b'250 AUTH PLAIN LOGIN XOAUTH2 OAUTHBEARER\r\n' 
reply: retcode (250); Msg: b'hermes--canary-production-ir2-f74ffc99c-2vqg6 Hello [192.168.1.8] 
[194.44.96.40])\nPIPELINING\nENHANCEDSTATUSCODES\n8BITMIME\nSIZE 41697280\nAUTH PLAIN LOGIN 
XOAUTH2 OAUTHBEARER'
send: 'AUTH PLAIN AGlib2pjdWtAeWFob28uY29tAGZhNDY1NTQxLg==\r\n'
reply: b'535 5.7.0 (#AUTH005) Too many bad auth attempts.\r\n'
reply: retcode (535); Msg: b'5.7.0 (#AUTH005) Too many bad auth attempts.'
send: 'AUTH LOGIN aWJvamN1a0B5YWhvby5jb20=\r\n'
send: 'QUIT\r\n'
Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\project1\mail_client.py", line 18, in <module>
    server.login(sender, password)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 739, in 
login
    (code, resp) = self.auth(
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 642, in 
auth
    (code, resp) = self.docmd("AUTH", mechanism + " " + response)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 432, in 
docmd
    return self.getreply()
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 405, in 
getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

问题分析

报错里的535 5.7.0 (#AUTH005) Too many bad auth attempts是核心问题:身份验证失败次数过多,触发了Yahoo的安全限制,导致服务器直接断开连接,最终抛出smtplib.SMTPServerDisconnected错误。主要原因包括:

  • Yahoo已禁用直接使用账户密码的SMTP登录方式,必须用应用专用密码
  • 代码存在冗余的连接调用,且邮件格式不符合标准(缺少必要邮件头)
  • 多次失败登录触发了账户安全拦截

解决步骤

1. 生成Yahoo应用专用密码

Yahoo现在要求开启两步验证后使用应用专用密码登录SMTP:

  • 登录Yahoo账户,进入「账户安全」页面
  • 开启「两步验证」(未开启的话必须先完成这一步)
  • 在「应用专用密码」选项中,创建一个新的专用密码,用于后续SMTP登录

2. 修改代码修复问题

原代码里的server.connect()属于冗余调用(with smtplib.SMTP()会自动建立连接),同时需要构造符合标准的邮件格式。修改后的代码如下:

import smtplib
from email.mime.text import MIMEText

sender = '你的yahoo邮箱@yahoo.com'
receiver = sender
# 替换成你生成的Yahoo应用专用密码
app_password = '你的应用专用密码'
port = 587

# 构造符合SMTP标准的邮件内容,必须包含邮件头
msg = MIMEText('just a test', 'plain', 'utf-8')
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = 'Test Email'

try:
    with smtplib.SMTP('smtp.mail.yahoo.com', port) as server:
        server.set_debuglevel(1)
        server.ehlo()
        server.starttls()
        server.ehlo()
        server.login(sender, app_password)
        print('logged in')
        server.sendmail(sender, receiver, msg.as_string())
        print('sent')
except Exception as e:
    print(f"Error: {e}")

3. Gmail相关补充(若需使用)

Gmail同样要求使用应用专用密码(开启两步验证后生成),SMTP服务器地址为smtp.gmail.com,端口587,代码结构和Yahoo一致,需确保邮件格式完整。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 23:54:05