python2.7 QQ邮件发送脚本

 

  使用qq的邮件服务器需要注意的两个地方主要是:

1.协议问题 使用465端口 SSL 协议

2.口令问题 出现SMTPAuthenticationError 主要的原因就是口令和帐号信息不对,这里我们使用qq服务器发送 需要先到邮箱里设置独立密码(必须), 然后开启

SMTP/POP3服务。然后用qq手机安全中心扫一扫会给一个授权码, 在代码中要填写的密码是这个 授权码 , 而不是邮箱密码!

下面贴个代码:

 1 #coding:utf8
 2 from smtplib import SMTP_SSL
 3 from email.header import Header
 4 from email.mime.text import MIMEText
 5 
 6 mail_info = {
 7     "from": "xxxx@qq.com",
 8     "to": "xxx@qq.com",
 9     "hostname": "smtp.qq.com",
10     "username": "xxx@qq.com",
11     "password": "xxx",
12     "mail_subject": "test",
13     "mail_text": "hello, this is a test email, sended by py",
14     "mail_encoding": "utf-8"
15 }
16 
17 if __name__ == '__main__':
18     #这里使用SMTP_SSL就是默认使用465端口
19     smtp = SMTP_SSL(mail_info["hostname"])
20     smtp.set_debuglevel(1)
21     
22     smtp.ehlo(mail_info["hostname"])
23     smtp.login(mail_info["username"], mail_info["password"])
24 
25     msg = MIMEText(mail_info["mail_text"], "plain", mail_info["mail_encoding"])
26     msg["Subject"] = Header(mail_info["mail_subject"], mail_info["mail_encoding"])
27     msg["from"] = mail_info["from"]
28     msg["to"] = mail_info["to"]
29     
30     smtp.sendmail(mail_info["from"], mail_info["to"], msg.as_string())
31 
32     smtp.quit()

 

posted @ 2016-04-15 16:20  1207270615  阅读(212)  评论(0)    收藏  举报