千纸鹤

  博客园  ::  :: 新随笔  ::  ::  :: 管理
  5 随笔 :: 70 文章 :: 0 评论 :: 9301 阅读
邮件应用:smtplib库(smtp是一种邮件传输协议,是对于smtp进行简单封装),邮件发送类型(文本邮件、html邮件、附件、图片)
《一》发送文本邮件的文件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 发送邮件(qq和163邮箱)
# 连接XX:smtp.XX.com(连接qq:smtp.qq.com、连接163邮箱:smtp.163.com)
# 连接邮箱服务器:smtplib.SMTP_SSL(邮箱地址,端口号)
con=smtplib.SMTP_SSL('smtp.qq.com','465')
# 密码授权:邮箱--设置--账户--开启smtp服务(字符复制下来作为密码)
con.login(user='321367834@qq.com',password='oggyfuunesapbjfb')

# 发送邮件(发送者账号和接受者账号)
sender='321367834@qq.com'
recevier=['321367834@qq.com','cemaxueyuan1@163.com']

# _text 邮件内容(_subtype:文件类型、plain:文本类型、_charset字符编码)
# 邮件正文:文本内容
message=MIMEText(_text='文本邮件',_subtype='plain',_charset='utf-8')

# 头部内容(标题)
message['Subject']=Header('文本邮件的标题')
message['From']=sender
message['to']=';'.join(recevier)

# 发送邮件(可能出现问题)
try:
con.sendmail(sender,recevier,message.as_string())
print('发送邮件成功')
except Exception as e:
print('ERROR 无法发送邮件')

《二》发送html邮件的文件
import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 发送邮件(qq和163邮箱)
# 连接XX:smtp.XX.com(连接qq:smtp.qq.com、连接163邮箱:smtp.163.com)
# 连接邮箱服务器:smtplib.SMTP_SSL(邮箱地址,端口号)
con=smtplib.SMTP_SSL('smtp.qq.com','465')
# 密码授权:邮箱--设置--账户--开启smtp服务(字符复制下来作为密码)
con.login(user='321367834@qq.com',password='oggyfuunesapbjfb')

# 发送邮件(发送者账号和接受者账号)
sender='321367834@qq.com'
recevier=['321367834@qq.com','cemaxueyuan1@163.com']

# html测试报告
htmlcontext='<a href="http://www.baidu.com">html邮件,点我进行跳转</a>'
# 邮件正文:html内容
message=MIMEText(_text=htmlcontext,_subtype='html',_charset='utf-8')

# 头部内容(标题)
message['Subject']=Header('html邮件的标题')
message['From']=sender
message['to']=';'.join(recevier)

# 发送邮件(可能出现问题)
try:
con.sendmail(sender,recevier,message.as_string())
print('发送邮件成功')
except Exception as e:
print('ERROR 无法发送邮件')

《三》发送附件的文件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart

# 发送邮件(qq和163邮箱)
# 连接XX:smtp.XX.com(连接qq:smtp.qq.com、连接163邮箱:smtp.163.com)
# 连接邮箱服务器:smtplib.SMTP_SSL(邮箱地址,端口号)
con=smtplib.SMTP_SSL('smtp.qq.com','465')
# 密码授权:邮箱--设置--账户--开启smtp服务(字符复制下来作为密码)
con.login(user='321367834@qq.com',password='oggyfuunesapbjfb')

# 发送邮件(发送者账号和接受者账号)
sender='321367834@qq.com'
recevier=['321367834@qq.com','cemaxueyuan1@163.com']

# 实例化附件:创建一个信封
message=MIMEMultipart()
# 发送html测试报告:设置可读取,html用二进制的文件读取
content=open('files/2020-11-16 21-15-18test_report.html','rb').read()
# 读取出来的内容写在文本中
file1=MIMEText(content,'base64','utf-8')
# 文本中取个名字
file1['Content-Disposition']='attachment;filename="fujian.html"'
# 把文本放在信封中去
message.attach(file1)
msg=MIMEText('这是一封附件的测试报告','plain','utf-8')
message.attach(msg)

# 头部内容(标题)
message['Subject']=Header('附件的标题')
message['From']=sender
message['to']=';'.join(recevier)

# 发送邮件(可能出现问题)
try:
con.sendmail(sender,recevier,message.as_string())
print('发送邮件成功')
except Exception as e:
print('ERROR 无法发送邮件')

《四》发送图片的文件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

# 发送邮件(qq和163邮箱)
# 连接XX:smtp.XX.com(连接qq:smtp.qq.com、连接163邮箱:smtp.163.com)
# 连接邮箱服务器:smtplib.SMTP_SSL(邮箱地址,端口号)
con=smtplib.SMTP_SSL('smtp.qq.com','465')
# 密码授权:邮箱--设置--账户--开启smtp服务(字符复制下来作为密码)
con.login(user='321367834@qq.com',password='oggyfuunesapbjfb')

# 发送邮件(发送者账号和接受者账号)
sender='321367834@qq.com'
recevier=['321367834@qq.com','cemaxueyuan1@163.com']

# 实例化附件:创建一个信封
message=MIMEMultipart()
# 打开图片
image1=open('files/imgae.jpg','rb').read()
image1_data=MIMEImage(image1)
# 取个名字
image1_data['Content-Disposition']='attachment;filename="aa.jpg"'
message.attach(image1_data)
msg=MIMEText('这是一封图片的测试报告','plain','utf-8')
message.attach(msg)

# 头部内容(标题)
message['Subject']=Header('图片的标题')
message['From']=sender
message['to']=';'.join(recevier)

# 发送邮件(可能出现问题)
try:
con.sendmail(sender,recevier,message.as_string())
print('发送邮件成功')
except Exception as e:
print('ERROR 无法发送邮件')
python之zmail的邮件发送
安装:pip install zmail
发送邮件的内容属性
Subject:标题
Content_text:邮件内容(文本类型)
Content_html:html邮件内容(网页类型)
Attachments:附件
代码示例
import zmail
mail_content={
"Subject":"邮件的标题",
"Content_text":"这是邮件内容",
"Content_html":'<a href="http://www.jd.com">我想逛京东了</a>',
"Attachments":'files/2020-11-16 21-15-18test_report.html'
}
# 发送人(发件人账号,发件人密码(授权码))
sender={'username':'321367834@qq.com','pwd':'oggyfuunesapbjfb'}
# 收件人
recevier=['321367834@qq.com','cemaxueyuan1@163.com']
# 登录邮箱
server=zmail.server(sender['username'],sender['pwd'])
# 服务器去发送邮件
server.send_mail(recevier,mail_content)
posted on   隆江猪脚饭  阅读(48)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示