python最简单发送邮件

 1 #!/usr/bin/env python
 2 #coding:utf8
 3 #Author:lsp
 4 #Date:下午5:51:13
 5 #Version:0.1
 6 #Function:
 7 
 8 #导入smtplib和MIMEText
 9 import smtplib, sys
10 from email.mime.text import MIMEText
11 
12 def send_mail(sub, content):
13     mailto_list = ["2933494044@qq.com"]
14     #设置服务器,用户名、口令以及邮箱的后缀
15     mail_host = "smtp.126.com"  #smtp.qq.com  mail.qq.com
16     mail_user = "li_shupeng@126.com"
17     mail_pass = "******"
18     mail_postfix = "qq.com"
19     me = mail_user + "<" + mail_user + ""  + ">"
20     msg = MIMEText(content, _charset='gbk')
21     msg['Subject'] = sub
22     msg['From'] = me
23     msg['To'] = ";".join(mailto_list)
24         
25     try:
26            s = smtplib.SMTP()
27            s.connect(mail_host)
28            s.login(mail_user, mail_pass)
29            s.sendmail(me, mailto_list, msg.as_string())
30            s.close()
31            return True
32     except Exception, e:
33         print str(e)
34         return False
35 if __name__ == '__main__':
36     if send_mail(u'这是python测试邮件', u'python发送邮件'):
37         print u'发送成功'
38     else:
39         print u'发送失败'

 

posted on 2016-06-16 19:20  ..小树苗  阅读(339)  评论(0编辑  收藏  举报

导航