码迷,mamicode.com
首页 > 编程语言 > 详细

python 发送邮件

时间:2015-01-19 10:43:32      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

 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     #构造附件1
11     #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     #构造附件2
17     #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_list 33 server.quit() 34 print 发送成功 35 except Exception, e: 36 print str(e) 37 if __name__ == __main__: 38 smtp(test)

 

python 发送邮件

标签:

原文地址:http://www.cnblogs.com/zenghui940/p/4233095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!