一: 前言
上一篇邮件主要是如何应对网上购买口罩通知到货不及时问题,详细请访问https://blog.csdn.net/muchong123/article/details/104146635
本篇主要重点总结下如何通过使用python库模块发送邮件(qq邮件,@163邮件等)
二:直接上代码
# smtplib 用于邮件的发信动作
import smtplib
# email 用于构建邮件内容
from email.mime.text import MIMEText
# 用于构建邮件头
from email.header import Header
#用于构建附件
from email.mime.multipart import MIMEMultipart
#用于构建图片
from email.mime.image import MIMEImage
class Mail:
'定义一个邮箱类'
#初始化发送方,接收方的邮箱信息
def __init__(self, MailServer, MailPort, FromMail, Authentication, ToMail):
self.mailserver = MailServer
self.mailport = MailPort
self.frommail = FromMail
self.password = Authentication
self.tomail = ToMail
def make_mail(self, head, content):
Msgs = content
Msgs['From'] = Header(self.frommail)
Msgs['To'] = Header(self.tomail)
Msgs['Subject'] = Header(head)
return Msgs
def send_mail(self, msgs):
print("开启邮件服务")
server = smtplib.SMTP_SSL(host=self.mailserver) # 开启发信服务,这里使用的是加密传输
#print("连接服务器")
server.connect(host=self.mailserver, port=self.mailport)
#print("登录服务器")
server.login(self.frommail, self.password)
#print("发送邮件")
server.sendmail(self.frommail, self.tomail, msgs.as_string())
print("发送邮件成功")
server.quit() # 关闭服务器
#构建普通文本内容的邮件
def build_text(msg, types):
mail_msg = MIMEText(msg, types, 'utf-8')
return mail_msg
#构建内容带网页链接的邮件
def build_html(types):
htmls = """
<p>发送html链接邮件</p>
<p><a href="https://blog.csdn.net/muchong123">欢迎访问我的博客主页!</a></p>
"""
mail_msg = MIMEText(htmls, types, 'utf-8')
return mail_msg
#构建内容带附件的邮件
def build_enclosure(types):
mail_msg = MIMEMultipart()
#构建邮件发送信息来源及目的
#构建邮件正文内容
mail_msg.attach(MIMEText('附件发送测试', types, 'utf-8'))
# 构造附件1,传送当前目录下的 test.txt 文件
att1 = MIMEText(open(r'C:\Users\Administrator\Desktop\test.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="test.txt"'
mail_msg.attach(att1)
return mail_msg
#构建html文本中带图片的邮件
def build_html_img(types):
msgRoot = MIMEMultipart('related')
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
mail_msg = """
<p>html+img链接邮件测试</p>
<p><a href="https://blog.csdn.net/muchong123">欢迎访问博客主页~~</a></p>
<p><img src="cid:image1"></p>
"""
msgAlternative.attach(MIMEText(mail_msg, types, 'utf-8'))
# 指定图片为当前目录
fp = open(r'C:\Users\Administrator\Desktop\wuhan2020.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# 定义图片 ID,在 HTML 文本中引用
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
return msgRoot
if __name__ == "__main__":
try:
#content = build_text('plain')
#content = build_html('plain')
#content = build_enclosure('plain')
content = build_html_img('html')
header = '发送邮件测试'
#print(content)
mail = Mail('smtp.qq.com', 465, '13501065xx@qq.com', 'zndzkclqgpobjfid', 'm1581xx87918@163.com')
print("构建邮件内容")
msgs = mail.make_mail(header, content)
print("发送邮件")
mail.send_mail(msgs)
print("邮件发送成功")
except Exception as e:
print("异常!!!")
这里只需要修改邮箱地址和授权码为您的即可,如果需要发送其它内容只需要改写或者重写邮件构建函数
运行结果如下:
- 发送文本内容
2.发送带html链接邮件
3.发送带附件邮件
4.发送html链接+图片邮件
本篇文章到此介绍完如何通过python库模块接口发送邮件的代码实现,希望对你们有帮助,谢谢!如果有不正确或者需要补充的地方,欢迎评论区留言!
来源:CSDN
作者:Chester_Carson
链接:https://blog.csdn.net/muchong123/article/details/104155797