码迷,mamicode.com
首页 > 编程语言 > 详细

Python发邮件的小脚本

时间:2017-05-12 01:41:11      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:logs   nec   als   except   else   port   hello   send   sendmail   

 1 # -*- coding: UTF-8 -*-
 2 import smtplib
 3 from email.mime.text import MIMEText
 4 
 5 mailto_list = [hitwh_Gypsy@126.com,hitwh_Gypsy@163.com,hitwh_Gypsy@aliyun.com,1027179157@qq.com]
 6 mail_host = "smtp.qq.com"  # 设置服务器
 7 mail_user = "1027179157"  # 用户名
 8 mail_pass = "xxxxxxxxx"  # 口令
 9 mail_postfix = "qq.com"  # 发件箱的后缀
10 
11 def send_mail(to_list, sub, content):
12     me = "hello" + "<" + mail_user + "@" + mail_postfix + ">"
13     msg = MIMEText(content, _subtype=plain, _charset=gb2312)
14     msg[Subject] = sub
15     msg[From] = me
16     msg[To] = ";".join(to_list)
17     try:
18         server = smtplib.SMTP()
19         server.connect(mail_host)
20         server.login(mail_user, mail_pass)
21         server.sendmail(me, to_list, msg.as_string())
22         server.close()
23         return True
24     except Exception as e:
25         print(str(e))
26         return False
27 
28 if __name__ == __main__:
29     if send_mail(mailto_list, "hello", "hello world!"):
30         print("发送成功")
31     else:
32         print("发送失败")

 

Python发邮件的小脚本

标签:logs   nec   als   except   else   port   hello   send   sendmail   

原文地址:http://www.cnblogs.com/standby/p/6843410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!