Python、Django : catch SMTPRecipientsRefused. 改为获取 "undefined variable"

标签 python django exception smtp

当我尝试发送电子邮件时出现以下异常:

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")}

我想捕获那个异常并尝试过:

try:
    sendActivationEmail()
except SMTPRecipientsRefused:
    return "error"

但是,这给了我一个“ undefined variable ”错误。它无法识别 SMTPRecipientsRefused。在 Eclipse 中它说“ undefined variable :SMTPRecipientsRefused”

最佳答案

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

关于Python、Django : catch SMTPRecipientsRefused. 改为获取 "undefined variable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13394681/

相关文章:

Python线程测试不起作用

python - Bokeh 中具有多个 x 轴刻度的水平条形图和折线图

django - 为什么使用 django TemplateTag 标签?

python - Django REST 框架 : how to make verbose name of field differ from its field_name?

php - 异常捕获 : when not to catch them?

java - Play Framework 异常 - 字段 .. 没有默认值

python - 使用 mx :RemoteObject with web2py's @service. amfrpc 装饰器

python - 为 Python + 一组依赖项创建 Windows 安装程序

django - 没有前端如何集成django-viewflow?

c++ - 函数体异常