本文基于qq邮箱服务来发送邮件,去邮箱的设置里去设置-账户下:
点击生成授权码(需要你的手机发送短信文字)获取到授权码后进行django的基本设置:
Settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.qq.com' # 如果是 163 改成 smtp.163.com EMAIL_PORT = 465 EMAIL_HOST_USER = 'xxxx@qq.com' # 在这里填入您的QQ邮箱账号 EMAIL_HOST_PASSWORD = 'xxxxxxxx' # 请在这里填上您自己邮箱的授权码 DEFAULT_FROM_EMAIL = EMAIL_HOST_USER EMAIL_USE_SSL = True
Views.py:
from django.shortcuts import HttpResponse from django.core.mail import send_mail from xxx.settings import dev def check_mail(request): msg = '确认已经收到的邮箱哦。' try: send_mail( subject='请注意这是Django邮件测试', message=msg, from_email=dev.EMAIL_HOST_USER, recipient_list=["xxxxx@163.com"]#接收者的邮箱账号 ) except Exception as e: return HttpResponese(e) else: return HttpResponese('测试邮件已发出请注意查收')