Python Django中QQ郵箱授權(quán)碼問題

系統(tǒng)及軟件版本如下:

  • Ubuntu Kylin 16.04
  • Python 3.5.1
  • Django 1.9.7
  • PyCharm Community Edition 2016.1.4

問題

在學(xué)習(xí)用Django寫一個博客的過程中,需添加一個郵件分享功能,在使用QQ郵箱發(fā)送郵件的時候碰到了問題。

在設(shè)置文件settings.py中添加以下設(shè)置:

EMAIL_HOST = 'smtp.qq.com'
EMAIL_HOST_USER = '2460490819@qq.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

使用manage.py提供的shell工具進行測試:

panzeyan@panzeyan-S400CA:~/PycharmProjects/myDjangoProject/mysite$ python3 manage.py shell
Python 3.5.1+ (default, Mar 30 2016, 22:46:26)
[GCC 5.3.1 20160330] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.mail import send_mail
>>> send_mail("learn django", "step by step", "2460490819@qq.com",['2460490819@qq.com'], fail_silently=False)

拋出異常:

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/shell.py", line 69, in handleself.run_shell(shell=options['interface'])
File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/shell.py", line 61, in run_shell
raise ImportError
ImportError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.5/code.py", line 91, in runcodeexec(code, self.locals)
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/django/core/mail/__init__.py", line 61, in send_mail
return mail.send()
File "/usr/local/lib/python3.5/dist-packages/django/core/mail/message.py", line 292, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python3.5/dist-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python3.5/dist-packages/django/core/mail/backends/smtp.py", line 67, in open
self.connection.login(self.username, self.password)
File "/usr/lib/python3.5/smtplib.py", line 729, in login
raise last_exceptionFile "/usr/lib/python3.5/smtplib.py", line 720, in login
initial_response_ok=initial_response_ok)
File "/usr/lib/python3.5/smtplib.py", line 641, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'Error: \xc7\xeb\xca\xb9\xd3\xc3\xca\xda\xc8\xa8\xc2\xeb\xb5\xc7\xc2\xbc\xa1\xa3\xcf\xea\xc7\xe9\xc7\xeb\xbf\xb4: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256')

思路和解決方法

忽略ImportError,關(guān)注SMTPAuthenticationError。猜測是QQ郵箱SMTP服務(wù)的授權(quán)問題導(dǎo)致發(fā)送郵件失敗。

打開異常末尾給出的鏈接:
http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

QQ郵箱需使用授權(quán)碼作為專用密碼,登錄第三方客戶端,所以用原來的密碼會導(dǎo)致錯誤。
按照鏈接中的提示,進入QQ郵箱設(shè)置頁面,開啟SMTP服務(wù),發(fā)短信獲取授權(quán)碼。然后在settings.py中將16位授權(quán)碼賦值給EMAIL_HOST_PASSWORD。

EMAIL_HOST_PASSWORD = 'shlvewmsheooebhd'

進入manage.py提供的shell工具進行測試:

>>> from django.core.mail import send_mail
>>> send_mail("1", "2", "2460490819@qq.com", ['2460490819@qq.com'], fail_silently=False)
1

1表示發(fā)送成功。登錄郵箱,成功收到郵件,問題解決。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容