标签:
1 # -*- coding: UTF-8 -*- 2 ‘‘‘ 3 发送txt文本邮件 4 http://www.cnblogs.com/liu-ke 5 ‘‘‘ 6 import smtplib 7 from email.mime.text import MIMEText 8 mailto_list=[‘***@**.***‘] 9 mail_host="smtp.****.com" #设置服务器 10 mail_user="***@**.**" #用户名 11 mail_pass="********" #口令 12 mail_postfix="***.com" #发件箱的后缀 13 14 def send_mail(to_list,sub,content): 15 me="hello"+"<"+mail_user+"@"+mail_postfix+">" 16 msg = MIMEText(content,_subtype=‘plain‘,_charset=‘gb2312‘) 17 msg[‘Subject‘] = sub 18 msg[‘From‘] = me 19 msg[‘To‘] = ";".join(to_list) 20 try: 21 server = smtplib.SMTP() 22 server.connect(mail_host) 23 server.login(mail_user,mail_pass) 24 server.sendmail(me, to_list, msg.as_string()) 25 server.close() 26 return True 27 except Exception, e: 28 print str(e) 29 return False 30 if __name__ == ‘__main__‘: 31 if send_mail(mailto_list,"hello","hello world!"): 32 print "发送成功" 33 else: 34 print "发送失败"
1 # -*- coding: utf-8 -*- 2 ‘‘‘ 3 发送html文本邮件 4 http://www.cnblogs.com/liu-ke 5 ‘‘‘ 6 import smtplib 7 from email.mime.text import MIMEText 8 mailto_list=["*****"] 9 mail_host="smtp.***.com" #设置服务器 10 mail_user="****" #用户名 11 mail_pass="****" #口令 12 mail_postfix="***.com" #发件箱的后缀 13 14 def send_mail(to_list,sub,content): #to_list:收件人;sub:主题;content:邮件内容 15 me="hello"+"<"+mail_user+"@"+mail_postfix+">" #这里的hello可以任意设置,收到信后,将按照设置显示 16 msg = MIMEText(content,_subtype=‘html‘,_charset=‘gb2312‘) #创建一个实例,这里设置为html格式邮件 17 msg[‘Subject‘] = sub #设置主题 18 msg[‘From‘] = me 19 msg[‘To‘] = ";".join(to_list) 20 try: 21 s = smtplib.SMTP() 22 s.connect(mail_host) #连接smtp服务器 23 s.login(mail_user,mail_pass) #登陆服务器 24 s.sendmail(me, to_list, msg.as_string()) #发送邮件 25 s.close() 26 return True 27 except Exception, e: 28 print str(e) 29 return False 30 if __name__ == ‘__main__‘: 31 if send_mail(mailto_list,"hello","<a href=‘http://www.cnblogs.com/xiaowuyi‘>小五义</a>"): 32 print "发送成功" ,mailto_list 33 else: 34 print "发送失败"
标签:
原文地址:http://www.cnblogs.com/liu-ke/p/4872195.html