首页 > 代码库 > 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.com25 msg[from] = ‘test@163.com26 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 发送邮件