使用python邮件加密:SSL加密: 端口号是465,通信过程加密,邮件数据安全02
# 编写人:Jaoany
# 开发时间:2021/7/19 14:57
# -*- conding:UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
# SSL加密: 端口号是465,通信过程加密,邮件数据安全
mail_server = "smtp.qq.com"
port = "465"
sender = "***@qq.com" # 发件人
password = "***" # 发件人smtp密码
receiver = ["***@163.com"] # 收信人
# 创建一个"正文+附件"的实例
massage = MIMEMultipart()
massage['From'] = Header(发件人昵称", "utf-8")
massage['To'] = Header("收件人昵称", "utf-8")
massage['Subject'] = Header("Python 学习资料", "utf-8")
# 正文内容
mail_msg = """
<p>Python3 学习教程...</p>
<p><a href="http://www.taitaiblog.com/43.html">Python 编程技术目录</a></p>
"""
# 邮件正文部分
massage.attach(MIMEText(mail_msg, "html", "utf-8"))
# 邮件附件部分
att= MIMEText(open('C:\\Users\Jaoany\Desktop\python发送电子邮件\my_file.txt', 'rb').read(), 'base64', 'gb2312')
att["Content-Type"] = 'application/octet-stream'
# filename 为邮件上显示的名称
att["Content-Disposition"] = 'attachment; filename="my_file.txt"'
massage.attach(att)
try:
mail = smtplib.SMTP_SSL(mail_server, port) # 连接邮箱服务器
status = mail.login(sender, password) # 登录
print(status)
mail.sendmail(sender, receiver, massage.as_string()) # 发送邮件
print("邮件发送成功!")
mail.quit() # 登出
except:
mail.quit()
print("邮件发送失败!")
本文来自博客园,作者:Jaoany,转载请注明原文链接:https://www.cnblogs.com/fanglijiao/p/15031413.html