python 发送邮件(网易邮箱)

#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
mailto_list=['[email protected]']#收件人(列表)
mail_host="smtp.163.com"#使用的邮箱的smtp服务器地址,这里是163的smtp地址
mail_user="[email protected]"#用户名(网易邮箱登录的用户名)
mail_pass="xxxxxx"#密码(注意:此处密码为网易邮箱授权码,获取方式看下图
def send_mail(to_list,sub,content):
    me="你好啊"+"<"+mail_user+">"
    msg = MIMEText(content,_subtype='plain')
    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 as e:
        print('未知错误')
        return False
for i in range(1):
    if send_mail(mailto_list,"你好","python邮件发送成功了666"):
            print ("发送成功!");
    else:
            print ("发送失败!");

python 发送邮件(网易邮箱)_第1张图片

然后执行python,如果收到邮件,那么恭喜你
python 发送邮件(网易邮箱)_第2张图片

 

你可能感兴趣的:(python)