首页 > 技术文章 > Python发送邮件

rainbow-tan 2020-12-14 11:31 原文

# coding=utf-8
import smtplib
from email.mime.text import MIMEText


def send_mail():
    send_account = 'xxxxx@qq.com'  # 发送方邮箱
    authorization_code = 'xxxxxxx'  # 填入发送方邮箱的授权码,可以去qq邮箱申请
    received_account = 'xxxxxxx@qq.com'  # 收件人邮箱

    msg = MIMEText("测试内容")  # 内容
    msg['Subject'] = "测试主题"  # 主题
    msg['From'] = send_account
    msg['To'] = received_account
    s = None
    try:
        s = smtplib.SMTP_SSL("smtp.qq.com", 465)  # 邮件服务器及端口号
        s.login(send_account, authorization_code)
        s.sendmail(send_account, received_account, msg.as_string())
        print("发送成功")
    except Exception as e:
        print("发送失败:{}".format(e))
    finally:
        if s is not None:
            s.quit()


if __name__ == '__main__':
    send_mail()

 

推荐阅读