当前位置:主页 > 软件编程 > Python代码 >

python3.4实现邮件发送功能

时间:2020-10-03 10:14:35 | 栏目:Python代码 | 点击:

本文实例为大家分享了python实现邮件发送功能的具体代码,供大家参考,具体内容如下

import smtplib 
import os 
from email.mime.text import MIMEText 
from email.mime.multipart import MIMEMultipart 
from email import encoders 
user = '*******@qq.com' 
pwd = '*******' 
to = ['******@139.com', '******@qq.com'] 
msg = MIMEMultipart() 
msg['Subject'] = '这里是主题...' 
content1 = MIMEText('这里是正文!', 'plain', 'utf-8') 
msg.attach(content1) 
attfile = 'C:\\Users\\hengli\\Pictures\\CameraMan\\哈哈.doc' 
basename = os.path.basename(attfile) 
fp = open(attfile, 'rb') 
att = MIMEText(fp.read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att.add_header('Content-Disposition', 'attachment',filename=('gbk', '', basename)) 
encoders.encode_base64(att) 
msg.attach(att) 
#----------------------------------------------------------- 
s = smtplib.SMTP('smtp.qq.com') 
s.login(user, pwd) 
s.sendmail(user, to, msg.as_string()) 
print('发送成功') 
s.close() 

您可能感兴趣的文章:

相关文章