这个版本,其实是借鉴网友的代码,做了部分修改,目前测试能用,但对于代码功能还不是很明白!先保存下吧!
#!/usr/bin/env python #coding=utf-8 #auther = shaw #ver = 0.8 import smtplib import sys import email.mime.text from email import encoders from email.Header import Header from email.utils import parseaddr,formataddr from time import sleep def slow(msg,text): print msg, for i in text: print i, sys.stdout.flush() sleep(0.8) return '.' # def _format_addr(s): name, addr = parseaddr(s) return formataddr(( \ Header(name, 'utf-8').encode(), \ addr.encode('utf-8') if isinstance(addr, unicode) else addr)) # mail_username = '[email protected]' mail_password = '123456' from_addr = mail_username to_addrs = ['[email protected]','[email protected]'] HOST = 'mail.shenfugroup.com' PORT = 25 smtp = smtplib.SMTP() slow('connecting','...') smtp.set_debuglevel(1) try: print smtp.connect(HOST,PORT) except: print '\033[31;2mCONNECT ERROR \033[0m****' smtp.starttls() try: slow('loginning','...') smtp.login(mail_username,mail_password) except Exception,e: print e #'\033[31;2mLOGIN ERROR \033[0m****' msg = email.mime.text.MIMEText("""Dear shaw: 小苹果 """,_subtype='plain', _charset='utf-8') msg['From'] = _format_addr(u'测试 <%s>' % from_addr) msg['To'] = ';'.join(to_addrs) msg['Subject']=Header('shaw', charset='utf-8') print msg.as_string() smtp.sendmail(from_addr,to_addrs,msg.as_string()) smtp.quit()