首页 > 代码库 > python 发送邮件
python 发送邮件
1 #!/usr/bin/python 2 # -*- coding: utf-8 -*- 3 def smtp(file): 4 from email.mime.text import MIMEText 5 from email.mime.multipart import MIMEMultipart 6 import smtplib 7 8 #msg=MIMEMultipart() 9 10 #构造附件111 #att1 = MIMEText(open(file, ‘rb‘).read(), ‘base64‘, ‘gb2312‘)12 #att1["Content-Type"] = ‘application/octet-stream‘13 #att1["Content-Disposition"] = ‘attachment; filename=‘+file#这里的filename可以任意写,写什么名字,邮件中显示什么名字14 #msg.attach(att1)15 16 #构造附件217 #att2 = MIMEText(open(‘d:\\123.txt‘, ‘rb‘).read(), ‘base64‘, ‘gb2312‘)18 #att2["Content-Type"] = ‘application/octet-stream‘19 #att2["Content-Disposition"] = ‘attachment; filename="123.txt"‘20 #msg.attach(att2)21 22 #加邮件头23 msg=MIMEText(file,_charset=‘utf-8‘)
#to_list=[‘jij@wangdaizhijia.com‘,‘taoj@wangdaizhijia.com‘,‘fanjj@wangdaizhijia.com‘] #发送给多个用户24 msg[‘to‘] = ‘zenghui@test.com‘25 msg[‘from‘] = ‘test@163.com‘26 msg[‘subject‘] = ‘备份‘27 #发送邮件28 try:29 server = smtplib.SMTP()30 server.connect(‘smtp.163.com‘)31 server.login(‘zenghui‘,‘123456‘)#XXX为用户名,XXXXX为密码32 server.sendmail(msg[‘from‘], msg[‘to‘],msg.as_string()) #如果要发送多个用户,把msg[‘to‘]替换成to_list33 server.quit()34 print ‘发送成功‘35 except Exception, e: 36 print str(e)37 if __name__ == ‘__main__‘:38 smtp(‘test‘)
python 发送邮件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。