加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-17 17:50:12 所属栏目:Python 来源:网络整理
导读:对python这个高级语言感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧! # @param python发送邮件实例 - 使用smtplib模块# @author 编程之家 jb51.cc|www.www.jb51.cc # 导入 smtplib 和 MIMEText import smtplib from email.mime.text im
对python这个高级语言感兴趣的小伙伴,下面一起跟随编程之家 52php.cn的小编两巴掌来看看吧!

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

# 导入 smtplib 和 MIMEText 
import smtplib 
from email.mime.text import MIMEText 
   
# 定义发送列表 
mailto_list=["root@52php.cn","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.52php.cn

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读