import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
# 第三方 SMTP 服务
# 设置服务器
mail_host = "smtp.qq.com"
# 用户名
mail_user = "1320685524@qq.com"
#获取授权码
mail_pass="mpaocydzpzfjidge"
# 发件人账号
sender = '1320685524@qq.com'
# 接收邮件,可设置为你的QQ邮箱或者其他邮箱
receivers = ['1320685524@qq.com']
message = MIMEMultipart("related")
# 发件人
message['From'] = Header("我是发件人", 'utf-8')
# 收件人
message['To'] = Header("我是收件人", 'utf-8')
subject = '美女图片蒋雪儿'
message['Subject'] = Header(subject, 'utf-8')
#带上图片:
msg = MIMEMultipart("alternative")
message.attach(msg)
send_content = """
<h1>天不生我李淳罡</h1>
<h1>剑道万古如长夜</h1>
<p>小二上酒</p>
<img src="cid:image">
<div>阅读请 <a href="https://www.37zw.net/0/761/">点我,点我</a></div>
"""
# 第一个参数为邮件内容
msg.attach(MIMEText(send_content, 'html', 'utf-8'))
# 读取当前目录下的图片
f = open('美女图片蒋雪儿.jpg', 'rb')
img_msg = MIMEImage(f.read())
f.close()
# 定义图片在HTML文本中的位置
img_msg.add_header('Content-ID', '<image>') # 根据id定位
message.attach(img_msg)
try:
smtpObj = smtplib.SMTP()
# 25 为 SMTP 端口号
smtpObj.connect(mail_host, 25)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")