Python 发送邮件模块smtplib、email

先导入几个模块smtplib、email

import smtplib
from email.mime.text import MIMEText


# 发送邮件函数
def send_mail():
    try:
        e_mail = smtplib.SMTP_SSL('smtp.163.com', 994)   # (域名,端口号)
        e_mail.login('XXX@163.com', '***')  # (发送邮箱,邮箱的授权码[具体见下图])
        msg = "这里是要发送的文本内容"
        msg = MIMEText(msg, "plain", 'utf-8')
        msg['Subject'] = '用Python发的'    # 邮件的标题
        msg['From'] = 'XXX@163.com'  # 发送人邮箱
        msg['To'] = 'XXX@qq.com'    # 收件人邮箱
        e_mail.sendmail('XXX@163.com', ['XXX@qq.com'], msg.as_string())
        # (发送人邮箱,收件人邮箱【可为列表】,字符串形式文本内容)
        e_mail.close()
        print("发送成功")
        return 1
    except Exception as msg:
        print("发送失败", msg)
        return 0


if __name__ == "__main__":
    send_mail()     # 调用发送函数