要想通过QQ邮箱来发送邮件,需要开启QQ邮箱的设置-账户里SMTP服务,接下来会通过发送短信验证来获得授权码,有了授权码后就可以在代码里添加了。
接下来看看QQ的邮件服务器配置:
(授权码填写在代码的password中即可)
import smtplib from email.MIMEText import MIMEText from email.Header import Header msg_from = 'xxx@qq.com' # 发送方邮箱 passwd = 'wnslrbepjjhvbbah' # 填入发送方邮箱的授权码 msg_to = 'xxx@qq.com' # 收件人邮箱 subject = "python邮件测试" # 主题 content = "这是我使用python smtplib及email模块发送的邮件" msg = MIMEText(content) msg['Subject'] = subject msg['From'] = msg_from msg['To'] = msg_to try: s = smtplib.SMTP_SSL("smtp.qq.com", 465)# 邮件服务器及端口号 s.login(msg_from, passwd) s.sendmail(msg_from, msg_to, msg.as_string()) print "发送成功" except s.SMTPException, e: print "发送失败" finally: s.quit()