学习笔记(32):21天通关Python(仅视频课)-案例实操:使用smtplib模块发送邮件

立即学习:https://edu.csdn.net/course/play/24797/282212?utm_source=blogtoedu

import smtplib, email.message, email.utils

fromaddr = '[email protected]'
password = '*****************'
first_cid = email.utils.make_msgid()
conn = smtplib.SMTP_SSL('smtp.qq.com', 465)
conn.set_debuglevel(10)
result = conn.login(fromaddr, password)
print('邮箱登录结果:', result)
msg = email.message.EmailMessage()
msg.set_content('<h1>python邮件发送</h1> ' +
                '<img src="cid:' + first_cid[1:-1] + '"/>' +
                '<font color="red" size="20px">测试文件</font>', 'html', 'UTF-8')
# 邮件的主题
msg['subject'] = 'python邮件测试'
# 发件人
msg['from'] = 'hello world <%s>' % fromaddr
# 收件人
msg['to'] = 'hello world <%s>' % '37*****[email protected]'
# 附件
with open('1.png', 'rb', True)as f:
    msg.add_attachment(f.read(), maintype='image', subtype='png', filename='itDay.png', cid=first_cid)
with open('1.png', 'rb', True)as f:
    msg.add_attachment(f.read(), maintype='image', subtype='png', filename='itDay.png')
conn.send_message(msg, fromaddr)
conn.quit()
发布了39 篇原创文章 · 获赞 29 · 访问量 895

猜你喜欢

转载自blog.csdn.net/happyk213/article/details/105271914
今日推荐