Python使用SSL方式发送QQ邮箱

 1 #coding:utf-8
 2 import smtplib
 3 from email.mime.text import MIMEText
 4 from email.header import Header
 5 
 6 # 第三方 SMTP 服务
 7 mail_host="smtp.qq.com"  #设置服务器
 8 mail_user="******@qq.com"    #用户名
 9 mail_pass="*********"   #口令,QQ邮箱是输入授权码,在qq邮箱设置 里用验证过的手机发送短信获得,不含空格
10 
11 
12 sender = '******qq.com'
13 receivers = ['******@qq.com','****@sina.com.cn']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
14 
15 message = MIMEText('a test for python', 'plain', 'utf-8')
16 message['From'] = Header("ppyy", 'utf-8')
17 message['To'] =  Header("you", 'utf-8')
18 
19 subject = 'my test'
20 message['Subject'] = Header(subject, 'utf-8')
21 
22 try:
23   smtpObj = smtplib.SMTP_SSL(mail_host, 465) 
24   smtpObj.login(mail_user,mail_pass)  
25   smtpObj.sendmail(sender, receivers, message.as_string())
26   smtpObj.quit()
27   print u"邮件发送成功"
28 except smtplib.SMTPException,e:
29   print e

 

posted on 2017-09-02 20:42  帅胡  阅读(251)  评论(0编辑  收藏  举报

导航