- 导包
from django.core.mail import send_mail
- 设置
# 默认配置是EMAIL_BACKEND配置为:django.core.mail.backends.locmem.EmailBackend
# 但是我们用smtp协议需要改成下面的配置
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend
# SMTP服务器域名
EMAIL_HOST = 'smtp.xxxxx.com'
# 服务端口,一般SMTP端口是25和425
EMAIL_PORT = 25
# 发送的邮箱账号
EMAIL_HOST_USER = 'Jrri@outlook.com'
# 在邮箱中设置的客户端授权密码
EMAIL_HOST_PASSWORD = 'PassWord'
#收件人看到的发件人 但是好像没有看到效果,也可以设置 EMAIL_FROM=EMAIL_HOST_USER
EMAIL_FROM = 'Jrri<Jrri@outlook.com>'
- 代码示例1
使用导入Django模块email,并使用
from django.core.mail import send_mail
subject = 邮件主题
message = '文字内容'
from_email = 发送人(同Setting/EMAIL_FROM)
recipient_list = [收件人1, 收件人2, ...]
html_message = '带有H5标签的消息内容'
send_mail(subject, message, from_email, recipient_list, html_message)
from django.core.mail import send_mail
from Blog import settings
from threading import Thread
t1 = Thread(target=send_mail,args=("通知","插入了%s"%comment,settings.EMAIL_HOST_USER,["123456789@qq.com"]),kwargs={"html_message":"<h2>测试一下</h2>"})
t1.start()