发送邮件前需要在文件头引入
from sgmllib import SGMLParser
import smtplib
from email.mime.text import MIMEText
from email.MIMEMultipart import MIMEMultipart
import datetime
#定义发送邮件的函数
def Send_Mail (Message):
TODAY = datetime.date.today()
CURRENTDAY=TODAY.strftime('%Y-%m-%d')
msg = MIMEMultipart()
'''
att = MIMEText(open(r'D:\report\aa.xlsx', 'rb').read(), 'base64', 'gb2312') #设置附件的目录
att['content-type'] = 'application/octet-stream'
att['content-disposition'] = 'attachment;filename="IMD_EBM.xlsx"' #设置附件的名称
msg.attach(att)
'''
content = str(Message) #正文内容
body = MIMEText(content,'plain','GBK') #设置字符编码
msg.attach(body)
msgto = ['123@126.com'] # 收件人地址 多个联系人,格式['aa@163.com'; 'bb@163.com']
msgfrom = '321@126.com' # 寄信人地址 ,
msg['subject'] = 'Finish at ' CURRENTDAY #主题
msg['date']=time.ctime() #时间
#msg['Cc']='bb@junbao.net' #抄送人地址 多个地址不起作用
mailuser = '321' # 用户名
mailpwd = '321' #密码
try:
smtp = smtplib.SMTP()
smtp.connect(r'smtp.126.com')# smtp设置
smtp.login(mailuser, mailpwd) #登录
smtp.sendmail(msgfrom, msgto, msg.as_string()) #发送
smtp.close()
print "success mail"
except Exception, e:
print e