python 使用smtplib模块发送邮件的简单示例

前端之家收集整理的这篇文章主要介绍了python 使用smtplib模块发送邮件的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对python这个高级语言感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!

# @param python发送邮件实例 - 使用smtplib模块
# @author 编程之家 jb51.cc|www.www.jb51.cc 

# 导入 smtplib 和 MIMEText 
import smtplib 
from email.mime.text import MIMEText 
   
# 定义发送列表 
mailto_list=["root@jb51.cc","10118157@qq.com"] 
   
# 设置服务器名称用户名、密码以及邮件后缀 
mail_host = "smtp.163.com"
mail_user = "xx@163.com"
mail_pass = "xx"
mail_postfix="163.com"
   
# 发送邮件函数 
def send_mail(to_list,sub,context): 
    '''''
    to_list: 发送给谁
    sub: 主题
    context: 内容
    send_mail("xxx@126.com","sub","context")
    '''
    me = mail_user + "<"+mail_user+"@"+mail_postfix+">"    msg = MIMEText(context)     msg['Subject'] = 'python email test'    msg['From'] = sub    msg['To'] = ";".join(to_list)     try:         send_smtp = smtplib.SMTP()
         send_smtp.connect(mail_host)
         send_smtp.login(mail_user,mail_pass)
         send_smtp.sendmail(me,to_list,msg.as_string()) 
        send_smtp.close()
         print 'success'
        return True
    except (Exception,e):         print(str(e)) 
        print 'false'  send_mail(mailto_list,"test mail","你好")

# End www.jb51.cc
原文链接:/python/526913.html

猜你在找的Python相关文章