【Python小随笔】使用加密方式进行QQ邮件发送

# 提示
def smtpSend(mail_msg):
    Q = "你的QQ号"
    # 邮箱服务器及认证信息
    mail_host = "smtp.qq.com"
    mail_user = f"{Q}@qq.com"
    mail_pass = "邮箱秘钥"
    # 发件人和收件人
    sender = f"{Q}@qq.com"
    recipients = [f"{Q}@qq.com"]
    # 构造邮件内容
    message = MIMEText(f"<p>{mail_msg}</p>", "html", "utf-8")
    message["From"] = formataddr((Header("发件人名字", "utf-8").encode(), sender))
    message["To"] = formataddr((Header("收件人名字", "utf-8").encode(), recipients[0]))
    message["Subject"] = Header("发送内容", "utf-8")
    # 连接邮件服务器并发送
    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)  # 使用加密连接
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, recipients, message.as_string())
        print("邮件发送成功")
    except smtplib.SMTPException as e:
        print("邮件发送失败", e)
    finally:
        smtpObj.quit()


posted @ 2024-12-06 15:05  PythonNew_Mr.Wang  Views(15)  Comments(0Edit  收藏  举报