我试图让Django发送电子邮件,但我收到了以下错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail
    return mail.send()
  File "/Library/Python/2.7/site-packages/django/core/mail/message.py", line 286, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/Library/Python/2.7/site-packages/django/core/mail/backends/smtp.py", line 92, in send_messages
    new_conn_created = self.open()
  File "/Library/Python/2.7/site-packages/django/core/mail/backends/smtp.py", line 59, in open
    self.connection.login(self.username, self.password)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 615, in login
    raise SMTPAuthenticationError(code, resp)
SMTPAuthenticationError: (534, '5.7.9 Application-specific password required. Learn more at\n5.7.9 http://support.google.com/accounts/bin/answer.py?answer=185833 v14sm3323298pbs.11 - gsmtp')

链接建议我执行两步验证,但仍然没有结果。
在my settings.py中
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'email@gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587

我尝试在Django Shell中测试这个,但得到了上面显示的错误:
>>> from django.core.mail import send_mail
>>> from django.conf import settings
>>> sub = "sup man"
>>> msg = "char lisss"
>>> from_user = settings.EMAIL_HOST_USER
>>> to = ["email@email.com"]
>>> send_mail(sub,msg,from_user,to, fail_silently=False)

最佳答案

因为您使用2因素认证,所以您必须为此应用程序创建一个密码,以便在不使用2因素认证的情况下访问您的Google帐户。
执行Google支持页面上的所有步骤以生成应用程序密码,然后更新您的电子邮件主机密码以使用该密码,而不是您的常规帐户密码。
在本页:support.google.com/accounts/answer/185833按照标题“如何生成应用程序密码”下的步骤操作。生成密码后,需要在配置中使用该密码。

关于python - 带有smtp.gmail的Django电子邮件SMTPAuthenticationError 534需要特定于应用程序的密码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28421887/

10-10 23:41
查看更多