python发送邮件

来源:互联网 发布:宝宝计划软件 编辑:程序博客网 时间:2024/06/06 03:29
 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 "发送失败"

 

0 0
原创粉丝点击