教程-Python例項-傳送郵件功能

weixin_33995481發表於2015-09-08

 

相關資料:

http://www.cnblogs.com/xiaowuyi

 

例項程式碼:

 1 import smtplib  
 2 from email.mime.text import MIMEText  
 3 mailto_list=["zhu@163.com.cn"] 
 4 mail_host=r"exchange.163.com.cn"  #設定伺服器
 5 mail_user="zhu"    #使用者名稱
 6 mail_pass="123456789"   #口令 
 7 mail_postfix="163.com.cn"  #發件箱的字尾
 8   
 9 def send_mail(to_list,sub,content):  
10     me="<"+mail_user+"@"+mail_postfix+">"  
11     msg = MIMEText(content,_subtype='plain',_charset='gb2312')  
12     msg['Subject'] = sub  
13     msg['From'] = me  
14     msg['To'] = ";".join(to_list)  
15     try:  
16         server = smtplib.SMTP()  
17         server.connect(mail_host)  
18         server.login(mail_user,mail_pass)  
19         server.sendmail(me, to_list, msg.as_string())  
20         server.close()  
21         return True  
22     except Exception, e:  
23         print str(e)  
24         return False  
25 if __name__ == '__main__':  
26     if send_mail(mailto_list,"hello","hello world!"):  
27         print "傳送成功"  
28     else:  
29         print "傳送失敗"

 

相關文章