Python smtplib 第二次连接失败
Python smtplib second connection failing
我正在尝试连接到我的邮件服务器,然后断开连接,然后在其他时间重新连接。初始连接有效,电子邮件已发送。我使用 quit() 断开连接,回复是关闭连接。当我再次尝试连接时,它会引发 SMTPServerDisconnected 请先 运行 connect()。我几年前发现这个问题是完全相同的问题 (https://bugs.python.org/issue22216),它表明它已修复。我现在遇到了同样的问题,不确定如何正确修复或关闭连接。
mail = smtplib.SMTP_SSL('mail.hostname.com', 465)
MAIL_PASS = 'password'
SENDER = 'name@hostname.com'
RECEIVER = 'receiver@something.com'
while True:
if (something)
mail.login(SENDER, MAIL_PASS)
mail.sendmail(SENDER, RECEIVER, 'test email')
mail.quit()
elif (something else)
mail.login(SENDER, MAIL_PASS)
mail.sendmail(SENDER, RECEIVER, 'test2 email')
mail.quit()
在第一封电子邮件和退出请求之后,它是这样说的:
reply: b'221 box5454.bluehost.com closing connection\r\n'
reply: retcode (221); Msg: b'box5454.bluehost.com closing connection'
下一次连接尝试是这样的:
send: 'ehlo [127.0.1.1]\r\n'
Traceback (most recent call last):
mail.login(SENDER, MAIL_PASS)
self.ehlo_or_helo_if_needed()
if not (200 <= self.ehlo()[0] <= 299):
self.putcmd(self.ehlo_msg, name or self.local_hostname)
self.send(str)
raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first
我找到了使该程序运行的方法。我正在使用 while True 来继续该程序。似乎当我在 True 之前为 smtplib 声明变量时,它有问题。我在 True 之后声明变量并且它有效。使用 while True 可能是一个菜鸟问题,但它让我抓狂,希望它能帮助其他人
MAIL_PASS = 'password'
SENDER = 'name@hostname.com'
RECEIVER = 'receiver@something.com'
while True:
mail = smtplib.SMTP_SSL('mail.hostname.com', 465)
if (something)
mail.login(SENDER, MAIL_PASS)
mail.sendmail(SENDER, RECEIVER, 'test email')
mail.quit()
elif (something else)
mail.login(SENDER, MAIL_PASS)
mail.sendmail(SENDER, RECEIVER, 'test2 email')
mail.quit()
我正在尝试连接到我的邮件服务器,然后断开连接,然后在其他时间重新连接。初始连接有效,电子邮件已发送。我使用 quit() 断开连接,回复是关闭连接。当我再次尝试连接时,它会引发 SMTPServerDisconnected 请先 运行 connect()。我几年前发现这个问题是完全相同的问题 (https://bugs.python.org/issue22216),它表明它已修复。我现在遇到了同样的问题,不确定如何正确修复或关闭连接。
mail = smtplib.SMTP_SSL('mail.hostname.com', 465)
MAIL_PASS = 'password'
SENDER = 'name@hostname.com'
RECEIVER = 'receiver@something.com'
while True:
if (something)
mail.login(SENDER, MAIL_PASS)
mail.sendmail(SENDER, RECEIVER, 'test email')
mail.quit()
elif (something else)
mail.login(SENDER, MAIL_PASS)
mail.sendmail(SENDER, RECEIVER, 'test2 email')
mail.quit()
在第一封电子邮件和退出请求之后,它是这样说的:
reply: b'221 box5454.bluehost.com closing connection\r\n'
reply: retcode (221); Msg: b'box5454.bluehost.com closing connection'
下一次连接尝试是这样的:
send: 'ehlo [127.0.1.1]\r\n'
Traceback (most recent call last):
mail.login(SENDER, MAIL_PASS)
self.ehlo_or_helo_if_needed()
if not (200 <= self.ehlo()[0] <= 299):
self.putcmd(self.ehlo_msg, name or self.local_hostname)
self.send(str)
raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first
我找到了使该程序运行的方法。我正在使用 while True 来继续该程序。似乎当我在 True 之前为 smtplib 声明变量时,它有问题。我在 True 之后声明变量并且它有效。使用 while True 可能是一个菜鸟问题,但它让我抓狂,希望它能帮助其他人
MAIL_PASS = 'password'
SENDER = 'name@hostname.com'
RECEIVER = 'receiver@something.com'
while True:
mail = smtplib.SMTP_SSL('mail.hostname.com', 465)
if (something)
mail.login(SENDER, MAIL_PASS)
mail.sendmail(SENDER, RECEIVER, 'test email')
mail.quit()
elif (something else)
mail.login(SENDER, MAIL_PASS)
mail.sendmail(SENDER, RECEIVER, 'test2 email')
mail.quit()