1
我已經在這方面搜索了一點點,不能拿出任何滿意的東西。Python的SMTP錯誤代碼處理
我一直在試圖編寫一個python程序來偵聽電子郵件反彈報告,並根據反彈原因以不同的時間間隔重新發送它們。
import smtplib
from smtplib import *
sender = '[email protected]'
receivers = ['[email protected]']
message = """From: From Arthur <[email protected]>
To: To Deep Thought <[email protected]>
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,'[email protected]')
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 [email protected] instead of [email protected])"
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)的實例獲取屬性所以我也試過,但我不確定通過它的構造函數(代碼,味精)是什麼參數。
請問有人能推動我朝着正確的方向發展嗎?
謝謝。
太謝謝你了!這工作。如果我理解正確,那麼通過在發生異常時隱式地將參數傳遞給構造函數來處理實例化,對嗎? –
你好。異常在引發時被實例化。我提供的代碼只是說python爲它創建一個名稱。屬性由異常類的構造函數設置。請看看[這個SO QA](http://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python) – Pynchia