[Python] python中发送邮件的代码片段 →→→→→进入此内容的聊天室

来自 , 2019-06-23, 写在 Python, 查看 104 次.
URL http://www.code666.cn/view/b49d4455
  1.  
  2. # -*- coding: UTF-8 -*-
  3. '''
  4. 发送txt文本邮件
  5. 小五义:http://www.cnblogs.com/xiaowuyi
  6. '''
  7. import smtplib  
  8. from email.mime.text import MIMEText  
  9. mailto_list=[YYY@YYY.com]
  10. mail_host="smtp.XXX.com"  #设置服务器
  11. mail_user="XXXX"    #用户名
  12. mail_pass="XXXXXX"   #口令
  13. mail_postfix="XXX.com"  #发件箱的后缀
  14.    
  15. def send_mail(to_list,sub,content):  
  16.     me="hello"+"<"+mail_user+"@"+mail_postfix+">"
  17.     msg = MIMEText(content,_subtype='plain',_charset='gb2312')  
  18.     msg['Subject'] = sub  
  19.     msg['From'] = me  
  20.     msg['To'] = ";".join(to_list)  
  21.     try:  
  22.         server = smtplib.SMTP()  
  23.         server.connect(mail_host)  
  24.         server.login(mail_user,mail_pass)  
  25.         server.sendmail(me, to_list, msg.as_string())  
  26.         server.close()  
  27.         return True
  28.     except Exception, e:  
  29.         print str(e)  
  30.         return False
  31. if __name__ == '__main__':  
  32.     if send_mail(mailto_list,"hello","hello world!"):  
  33.         print "发送成功"
  34.     else:  
  35.         print "发送失败"
  36.  
  37.  
  38. #//python/5024

回复 "python中发送邮件的代码片段"

这儿你可以回复上面这条便签

captcha