一、实验环境
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 = ‘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邮箱查看收件箱
原文地址:https://www.cnblogs.com/hester/p/10540553.html
时间: 2024-10-27 12:43:50