Python发邮件的小脚本

 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("发送失败")

 

posted @ 2017-05-11 23:18  lixin[at]hitwh  阅读(256)  评论(0编辑  收藏  举报