gpt4 book ai didi

Python SMTP 错误代码处理

转载 作者:行者123 更新时间:2023-11-30 23:04:47 27 4
gpt4 key购买 nike

我对此进行了相当多的搜索,但找不到任何令人满意的结果。

我一直在尝试编写一个Python程序来监听电子邮件退返回告,并根据退回的原因以不同的时间间隔重新发送它们。

import smtplib
from smtplib import *

sender = 'foo@bar.com'
receivers = ['42@life.com']

message = """From: From Arthur <foo@bar.com>
To: To Deep Thought <42@life.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""

try:
smtpObj = smtplib.SMTP('smtp.gmail.com',587)
smtpObj.starttls()
smtpObj.login(sender,'foo@bar.com')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPResponseException:
error_code = SMTPResponseException.smtp_code
error_message = SMTPResponseException.smtp_error
print "Error code:"+error_code
print "Message:"+error_message
if (error_code==422):
print "Recipient Mailbox Full"
elif(error_code==431):
print "Server out of space"
elif(error_code==447):
print "Timeout. Try reducing number of recipients"
elif(error_code==510 or error_code==511):
print "One of the addresses in your TO, CC or BBC line doesn't exist. Check again your recipients' accounts and correct any possible misspelling."
elif(error_code==512):
print "Check again all your recipients' addresses: there will likely be an error in a domain name (like mail@domain.coom instead of mail@domain.com)"
elif(error_code==541 or error_code==554):
print "Your message has been detected and labeled as spam. You must ask the recipient to whitelist you"
elif(error_code==550):
print "Though it can be returned also by the recipient's firewall (or when the incoming server is down), the great majority of errors 550 simply tell that the recipient email address doesn't exist. You should contact the recipient otherwise and get the right address."
elif(error_code==553):
print "Check all the addresses in the TO, CC and BCC field. There should be an error or a misspelling somewhere."
else:
print error_code+": "+error_message

我收到以下错误:

Traceback (most recent call last): File "C:/Users/Varun Shijo/PycharmProjects/EmailBounce/EmailBounceTest.py", line 20, in error_code = SMTPResponseException.smtp_code AttributeError: type object 'SMTPResponseException' has no attribute 'smtp_code'

我在某处读到,我应该尝试从 SMTPResponseException 类的实例获取属性(即使 smtplib 文档说 otheriwse),所以我也尝试了,但我不确定传递其构造函数的参数是什么(代码,消息)。

有人可以把我推向正确的方向吗?

谢谢。

最佳答案

尝试

except SMTPResponseException as e:
error_code = e.smtp_code
error_message = e.smtp_error

关于Python SMTP 错误代码处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33545321/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com