import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header

# 收发人
sender = '[email protected]'
password = 'uaimpajcklvecahc'
receivers = ["[email protected]", "[email protected]", "[email protected]"]  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

# 构建根对象MIMEMultipart
msgRoot = MIMEMultipart()
msgRoot['Subject'] = '开门,劳资是网页图片1035'
msgRoot['From'] = sender
msgRoot['To'] = ";".join(receivers)

# 构建【文本对象】MIMEMultipart
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.baidu.com">百度一下你就知道</a></p>
<p>图片演示:</p>
<p><img src="cid:image1"></p>
"""
msgRoot.attach(MIMEText(mail_msg, 'html', 'utf-8'))


# 构建【图片对象】MIMEImage
msgImage = MIMEImage(open('./res/meinvb.jpg', 'rb').read())
msgImage.add_header('Content-ID', '<image1>')# 定义图片 ID,在 HTML 文本中引用
msgRoot.attach(msgImage)

try:
    # 登录邮件服务器
    smtpObj = smtplib.SMTP_SSL()
    smtpObj.connect('smtp.qq.com', 465)
    smtpObj.login(sender, password)

    # 发送并退出
    smtpObj.sendmail(sender, receivers, msgRoot.as_string())
    smtpObj.quit()
    print("邮件发送成功")

# 处理异常
except smtplib.SMTPException as e:
    print("Error: 无法发送邮件", e)

results matching ""

    No results matching ""