赞
踩
步骤:
示例:
import smtplib
import email.message
fromaddr = 'wk_helloworld@qq.com' # 账号
password = '****************' # QQ授权码
conn = smtplib.SMTP_SSL('smtp.qq.com', 465) # 创建SMTP连接
conn.login(fromaddr, password) # 登录邮件服务器
msg = email.message.EmailMessage() # 创建邮件对象
msg.set_content('您好,Python邮件') # 设置邮件内容(普通邮件)
conn.sendmail(fromaddr, ['929667257@qq.com'], msg.as_string()) # 发送邮件
conn.quit() # 退出连接
import smtplib import email.message fromaddr = 'wk_helloworld@qq.com' password = '****************' conn = smtplib.SMTP_SSL('smtp.qq.com', 465) conn.login(fromaddr, password) msg = email.message.EmailMessage() msg.set_content('<h2>HTML邮件<h2>' + '<div style="border:1px:solid red">HTML邮件内容</div>', 'html', 'UTF-8') msg['subject'] = 'HTML邮件' msg['from'] = '痴迷<%s>' % fromaddr msg['to'] = '淡然<%s>' % '929667257@qq.com' conn.sendmail(fromaddr, ['929667257@qq.com'], msg.as_string()) conn.quit()
import smtplib import email.message import email.utils fromaddr = 'wk_helloworld@qq.com' password = '****************' toaddr = '929667257@qq.com' conn = smtplib.SMTP_SSL('smtp.qq.com', 465) conn.login(fromaddr, password) msg = email.message.EmailMessage() first_id = email.utils.make_msgid() msg.set_content('<h2>HTML邮件<h2>' + '<div style="border:1px:solid red">html邮件内容</div>' + '<img src="cid:' + first_id[1:-1] + '">', 'html', 'UTF-8') msg['subject'] = 'HTML邮件' msg['from'] = 'wk<%s>' % fromaddr msg['to'] = 'k<%s>' % toaddr # 添加附件 with open('图1.jpg', 'rb') as f: # 附件指定cid后,邮件正文可通过该cid来引用该图片 msg.add_attachment(f.read(), maintype='image', subtype='jepg', filename='test1.jpg', cid=first_id) with open('图2.jpg', 'rb') as f: msg.add_attachment(f.read(), maintype='image', subtype='jepg', filename='test2.jpg') # with open('图3.gif', 'rb') as f: # msg.add_attachement(f.read(), maintype='image', subtype='gif', filename='test.jpg') conn.sendmail(fromaddr, [toaddr], msg.as_string()) conn.quit()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。