1. 程式人生 > >Python Django 配置QQ郵箱發送郵件

Python Django 配置QQ郵箱發送郵件

highlight ati cert 設置 als quic smtp credit faq

一、實驗環境

Python2.7 + Django1.10.0

二、獲取QQ郵箱授權碼

1、什麽是授權碼?

授權碼是QQ郵箱推出的,用於登錄第三方客戶端的專用密碼。 適用於登錄以下服務:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務。 溫馨提醒:為了你的帳戶安全,更改QQ密碼以及獨立密碼會觸發授權碼過期,需要重新獲取新的授權碼登錄。

2、怎麽獲取授權碼?

先進入設置-》帳戶頁面找到入口,按照以下流程操作。 (1)點擊“開啟” 技術分享圖片 (2)驗證密保 技術分享圖片 (3)獲取授權碼 技術分享圖片

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

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

三、Django中配置

# 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 = ‘[email protected]‘
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 = ‘[email protected]

進入shell界面

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 = ‘[email protected]‘
In [5]: send_mail(email_title,email_body,‘[email protected]‘,[email])

如果返回值為1表示發送成功,登錄163郵箱查看收件箱

Python Django 配置QQ郵箱發送郵件