python 使用 126 邮箱发送邮件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/4/21 23:12
# @Author  : xhzheng
# @Email   : [email protected]
# @File    : TestSendMail.py
# @Software: PyCharm

import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time


def sendMail(sender, receivers, mail_content):
    nowTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
    message = MIMEText(nowTime + '\n' + mail_content, 'plain', 'utf-8')
    message['Subject'] = Header("每日信息报告", 'utf-8')
    message['From'] = str("[email protected]")  # 发送者
    message['To'] = str("lucky every day")  # 接收者

    smtpObj = smtplib.SMTP()
    smtpObj.connect("smtp.126.com")
    try:
    	# 126的密码需要自己去126的设置里面查找
        smtpObj.login("[email protected]", "【126的密码】")
    except:
        print("smtp login error")

    smtpObj.sendmail("[email protected]", receivers, message.as_string())
    smtpObj.quit()


if __name__ == '__main__':
    mail_content = "今天的天气不错,lucky!"
    receivers = ["[email protected]"]
    sender = "[email protected]"
    sendMail(sender, receivers, mail_content)

你可能感兴趣的:(Python)