参考:http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
1. 进入qq邮箱
2. 点击设置 -- 账户,此页面往下拉,开启授权码
按上面提示,发短信后,就可以获取到授权码
# -*- coding: UTF-8 -*- import time import smtplib from email.mime.text import MIMEText from email.header import Header #什么是授权码,它又是如何设置? http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256 def send_email(subject, msg): now = time.strftime("%Y%m%d-%H-%M", time.localtime()) # qq邮件 授权码 (改密码后会过期,需要从新获取授权码) pwd = "milwfffffdsssssdacg" # 发送方 sender = "1970664163@qq.com" # 收件方 # receivers = ["1738273291@qq.com", '1970664163@qq.com'] receivers = ["1738273291@qq.com"] # 发信服务器 smtp_server = 'smtp.qq.com' msg = MIMEText(msg, 'html', "utf-8") # html格式 msg['From'] = Header("stock web", 'utf-8') # msg['To'] = Header("xiakeyun", 'utf-8') # 主题 subject = f"[{now}]:{subject}" msg["Subject"] = Header(subject) # 开启发信服务,这里使用加密传输 server = smtplib.SMTP_SSL(smtp_server) server.connect(smtp_server) # 发送邮件 server.login(sender, pwd) server.sendmail(sender, receivers, msg.as_string()) server.quit() if __name__ == '__main__': send_email("测试邮件功能", "msg")