# -*- coding:utf-8 -*-
from email.mime.text import MIMEText
from email.header import Header
import smtplib

def send_mail(file_new):
f = open(file_new,'rb')
mail_body = f.read()
f.close()

msg = MIMEText(mail_body,'html','utf-8') 
msg['Subject'] = Header("自动化测试报告",'utf-8')

smtp = smtplib.SMTP()
smtp.connect("smtp.126.com")
smtp.login("username@126.com",'password')
smtp.sendmail("username@126.com","receive@126.com",msg.as_string())
smtp.quit()
print "email has send out !"

send_mail("D:\\123456.txt")

注:以上代码可以实现自动发送邮件,不过在发送中文或者中英混合的文件时,会出现乱码的情况。对于主题、回复人涉及汉字的,要用Header("xxxx","utf-8")方式进行编码转换。至于内容,就不要转换成utf-8了。

相关文章:

  • 2021-06-03
  • 2021-06-07
  • 2021-09-13
  • 2022-12-23
  • 2021-06-24
  • 2021-12-30
  • 2022-12-23
  • 2022-01-30
猜你喜欢
  • 2021-05-23
  • 2021-04-02
  • 2022-12-23
  • 2021-09-17
  • 2022-02-09
  • 2022-12-23
相关资源
相似解决方案