python:smtplib.SMTPServerDisconnected:连接意外关闭

时间:2019-01-19 11:02:12

标签: python-3.x smtplib

我正在尝试向特定的人发送电子邮件,但是代码抛出错误

import openpyxl,smtplib,sys

w=openpyxl.load_workbook('d.xlsx')
she=w.get_sheet_by_name('Sheet1')
lc=she.max_column
lm=she.cell(row=1,column=lc).value
u={}
for r in range(2,she.max_row+1):
    pay=she.cell(row=r,column=lc).value
    if pay!='paid':
        name=she.cell(row=r,column=1).value
        email=she.cell(row=r,column=2).value
        u[name]=email

smtp=smtplib.SMTP('smtp.mail.yahoo.com',465)
smtp.ehlo()
smtp.login('my_email@yahoo.com',sys.argv[1])

for n,e in u.items():
    b="Subject:%s dues unpaid.\nDear %s,\n Records show"%(lm,n)
    print('sending email to %s...'%(e))
    sm=smtp.sendmail('my_email@yahoo.com',e,b)

    if sm!={}:
        print('there was a problem sending email to %s:%s'%(e,sm))
smtp.quit()

上面的代码产生以下错误

 Traceback (most recent call last):
  File "cc.py", line 16, in <module>
    smtp=smtplib.SMTP('smtp.mail.yahoo.com',465)
  File "/usr/lib/python3.6/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.6/smtplib.py", line 338, in connect
    (code, msg) = self.getreply()
  File "/usr/lib/python3.6/smtplib.py", line 394, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

我已经用谷歌搜索了这个问题,但没有得到任何答案

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,以下变通办法为我解决了这个问题: 通过使用:

smtp.set_debuglevel(1)

我发现以下服务器回复中显示了yahoo服务器拒绝连接的根本原因:

  

回复:b'535 5.7.0(#AUTH005)错误的身份验证尝试太多。\ r \ n'

如果您遇到相同的问题,那么here会很好地说明解决方案。