#coding=utf8 os.chdir(sys.path[0]) #配置信息 authInfo = {} authInfo['server'] = 'smtp.163.com' authInfo['user'] = '[email protected]' authInfo['password'] = '2JlP' fromAdd = '[email protected]' #toAdd = ['[email protected]','[email protected]'] subject = '邮件主题' plainText = '这里是普通文本' htmlText = '<DIV><B>HTML文本</B></DIV>' name='张三' #别人看到的你的名字 tno=5 #线程数 q=Queue.Queue() jpg='test.jpg' #图像附件文件名 doc='doc.docx' #非图像音频文件名 html='' addlist='lis.txt' #邮件列表 import email,os,sys,threading,smtplib,base64,Queue,time from email.MIMEMultipart import MIMEMultipart from email.header import Header from email.MIMEText import MIMEText from email.MIMEImage import MIMEImage from email.mime.application import MIMEApplication class smail(threading.Thread): def __init__(self,authInfo,fromAdd,q,subject,plainText,htmlText,name): super(smail,self).__init__() self.authInfo=authInfo self.fromAdd=fromAdd self.q=q self.subject=subject self.plainText=plainText self.htmlText=htmlText self.name=name def run(self): while True: try: strFrom = self.fromAdd if self.q.empty(): break strTo = self.q.get(timeout=4) server = self.authInfo.get('server') user = self.authInfo.get('user') passwd = self.authInfo.get('password') if not (server and user and passwd) : print 'incomplete login info, exit now' return # 设定root信息 msgRoot = MIMEMultipart('related') msgRoot['Subject'] = Header(self.subject, 'utf8') msgRoot['From'] = '=?utf8?B?%s?= <%s>' % (base64.b64encode(self.name),strFrom) msgRoot['To'] = strTo msgRoot.preamble = 'This is a multi-part message in MIME format.' # Encapsulate the plain and HTML versions of the message body in an # 'alternative' part, so message agents can decide which they want to display. msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) #设定纯文本信息 msgText = MIMEText(self.plainText, 'plain', 'utf-8') msgAlternative.attach(msgText) #设定HTML信息 msgText = MIMEText(self.htmlText, 'html', 'utf-8') msgAlternative.attach(msgText) #设定内置图片信息,如果不需要可以注解掉 fp = open(jpg, 'rb') msgImage = MIMEImage(fp.read()) fp.close() msgImage.add_header('Content-ID', '<%s1>' % jpg) msgImage.add_header('Content-Disposition', 'attachment; filename=%s' % jpg) msgImage.set_param('name',jpg) msgRoot.attach(msgImage) #设定其他附件 fp=open(doc,'rb') msgf=MIMEApplication(fp.read()) fp.close() msgf.add_header('Content-ID', '<%s1>' % doc) msgf.add_header('Content-Disposition', 'attachment; filename="%s"' % doc) msgf.set_param('name',doc) msgRoot.attach(msgf) #发送邮件 smtp = smtplib.SMTP() #smtp.set_debuglevel(1) smtp.connect(server) smtp.login(user, passwd) smtp.sendmail(strFrom, strTo, msgRoot.as_string()) smtp.quit() print 'sending '+strTo except Exception as ad: print ad pass for i in open(addlist).readlines(): q.put(i.strip()) for i in xrange(tno): smail(authInfo,fromAdd,q,subject,plainText,htmlText,name).start() time.sleep(30)