python 简单的发送email方法

来源:互联网 发布:数据与信息的关系 编辑:程序博客网 时间:2024/05/19 18:10
# -*- coding: UTF-8 -*-'''发送txt文本邮件小五义:http://www.cnblogs.com/xiaowuyi'''import smtplibfrom email.mime.text import MIMETextmailto_list=["xxxx.com"]mail_host="smtp.xxxx.com"  #设置服务器mail_user="xxxx"    #用户名mail_pass="xxxx"   #口令mail_postfix="xxxx.com"  #发件箱的后缀def send_mail(to_list,sub,content):    me="hello"+"<"+mail_user+"@"+mail_postfix+">"    msg = MIMEText(content,_subtype='plain',_charset='UTF-8')    msg['Subject'] = sub    msg['From'] = me    msg['To'] = ";".join(to_list)    try:        server = smtplib.SMTP()        server.connect(mail_host)        server.login(mail_user,mail_pass)        server.sendmail(me, to_list, msg.as_string())        server.close()        return True    except Exception, e:        print str(e)        return Falseif __name__ == '__main__':    if send_mail(mailto_list,"hello","hello world!"):        print "发送成功"    else:        print "发送失败"

0 0