邮件发送

 1 import smtplib
 2 from email.mime.text import MIMEText
 3 from email.header import Header
 4 
 5 # 第三方 SMTP 服务
 6 mail_host="smtp.sina.com"  #设置服务器
 7 mail_user="#####@sina.com"    #用户名
 8 mail_pass="#######"   #口令
 9 
10 
11 sender = 'qkkspace@sina.com'
12 receivers = ['136503868@qq.com','940788158@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
13 print(receivers[0:])
14 
15 message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')
16 message['From'] = Header(sender, 'utf-8')
17 message['To'] =  Header(",".join(receivers), 'utf-8')
18 
19 subject = 'Python SMTP 邮件测试'
20 message['Subject'] = Header(subject, 'utf-8')
21 
22 
23 try:
24     smtpObj = smtplib.SMTP()
25     smtpObj.connect(mail_host, 25)    # 25 为 SMTP 端口号
26     smtpObj.login(mail_user,mail_pass)
27     smtpObj.sendmail(sender, receivers, message.as_string())
28     print("邮件发送成功")
29 except smtplib.SMTPException:
30     print("Error: 无法发送邮件")

 

posted on 2016-12-13 14:56  enet01  阅读(101)  评论(0)    收藏  举报