Python Django 配置QQ邮箱发送邮件
一、实验环境
Python2.7 + Django1.10.0
二、获取QQ邮箱授权码
1、什么是授权码?
授权码是QQ邮箱推出的,用于登录第三方客户端的专用密码。
适用于登录以下服务:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务。
温馨提醒:为了你的帐户安全,更改QQ密码以及独立密码会触发授权码过期,需要重新获取新的授权码登录。
2、怎么获取授权码?
先进入设置-》帐户页面找到入口,按照以下流程操作。
(1)点击“生成授权码”
![](https://service.mail.qq.com/images/faq/76FD1EA3-AC06-4938-9E2F-E6789AA04996.jpeg)
(2)验证密保
![](https://service.mail.qq.com/images/faq/5FE40CAA-12A3-4C82-AA2F-9B8BC75C3B4E.jpeg)
(3)获取授权码
![](https://service.mail.qq.com/images/faq/CF53747E-4FC8-4907-8550-F6A65B96BA07.jpeg)
3、在第三方客户端怎么设置?
在第三方客户端的密码框里面输入16位授权码进行验证。
三、Django中配置
setting.py中添加如下代码
# 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界面
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邮箱查看收件箱