立即学习: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()