Python,Django:捕获SMTPRecipientsRefused.获取"未定义变量"

use*_*003 1 python django smtp exception-handling

我尝试发送电子邮件时收到以下异常:

Traceback (most recent call last):
  my_own_functions ...
  File "C:\Python27\lib\site-packages\django\core\mail\message.py", line 248, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "C:\Python27\lib\site-packages\django\core\mail\backends\smtp.py", line 92, in send_messages
    sent = self._send(message)
  File "C:\Python27\lib\site-packages\django\core\mail\backends\smtp.py", line 110, in _send
    email_message.message().as_string())
  File "C:\Python27\lib\smtplib.py", line 733, in sendmail
    raise SMTPRecipientsRefused(senderrs)
SMTPRecipientsRefused: {'aaa': (553, "5.1.2 We weren't able to find the recipient domain. Please check for any\n5.1.2 spelling errors, and make sure you didn't enter any spaces, periods,\n5.1.2 or other punctuation after the recipient's email address. q10sm6381464wie.2")}
Run Code Online (Sandbox Code Playgroud)

我想抓住那个例外并尝试过:

try:
    sendActivationEmail()
except SMTPRecipientsRefused:
    return "error"
Run Code Online (Sandbox Code Playgroud)

但是,这给了我一个"未定义的变量"错误.它无法识别SMTPRecipientsRefused.在Eclipse中它显示"Undefined variable:SMTPRecipientsRefused"

Dan*_*man 6

异常与Python中的任何其他类一样:您需要先将其导入代码,然后才能引用它.你需要from smtplib import SMTPRecipientsRefused.