Python Django 配置QQ郵箱發送郵件

1、實驗環境

Python2.7 + Django1.10.0python

2、獲取QQ郵箱受權碼

一、什麼是受權碼?

受權碼是QQ郵箱推出的,用於登陸第三方客戶端的專用密碼。
適用於登陸如下服務:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務。
舒適提醒:爲了你的賬戶安全,更改QQ密碼以及獨立密碼會觸發受權碼過時,須要從新獲取新的受權碼登陸。
 

二、怎麼獲取受權碼?

先進入設置-》賬戶頁面找到入口,按照如下流程操做。
(1)點擊「生成受權碼」
(2)驗證密保
(3)獲取受權碼

三、在第三方客戶端怎麼設置?

在第三方客戶端的密碼框裏面輸入16位 受權碼進行驗證。

3、Django中配置

setting.py中添加以下代碼shell

# Host for sending email.
EMAIL_HOST = 'smtp.qq.com'

# Port for sending email.
EMAIL_PORT = 587

# Optional SMTP authentication information for EMAIL_HOST.
EMAIL_HOST_USER = 'gjp_1988@qq.com'
EMAIL_HOST_PASSWORD = '獲取的受權碼'
EMAIL_USE_TLS = True              #必須爲True
EMAIL_USE_SSL = False
EMAIL_SSL_CERTFILE = None
EMAIL_SSL_KEYFILE = None
EMAIL_TIMEOUT = None

# Default email address to use for various automated correspondence from
# the site managers.
DEFAULT_FROM_EMAIL = 'gjp_1988@qq.com'

 進入shell界面django

hester@hester-virtual-machine:~/django_project/sample1/mysite$ ./manage.py shell
fPython 2.7.12 (default, Nov 12 2018, 14:36:49) 
Type "copyright", "credits" or "license" for more information.

IPython 2.4.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

 

In [1]: from django.core.mail import send_mail
In [2]: email_title = 'from hester'
In [3]: email_body = 'password reset'
In [4]: email = 'gjp_1988@163.com'
In [5]: send_mail(email_title,email_body,'gjp_1988@qq.com',[email])

 

 若是返回值爲1表示發送成功,登陸163郵箱查看收件箱安全

相關文章
相關標籤/搜索