- 论坛徽章:
- 2
|
本帖最后由 tiankafeiwu 于 2012-07-10 17:51 编辑
我是新手,对于很多地方运用还不是很到位,贴出来欢迎大家一起讨论和改进,还有不少地方需要改进。- #-*- encoding: utf-8 -*-
- import threading
- import time,datetime
- import sys, os
- import smtplib
- from email.mime.text import MIMEText
- import Queue
- '''
- list.txt文件格式:
- aaa@163.com
- bbb@yahoo.com.cn
- ccc@qq.com
- ddd@126.com
- '''
- sub="这里是邮件的主题subject"
- content="这里是邮件的内容content"
- #####################
- #设置服务器,用户名、口令以及邮箱的后缀
- mail_host="smtp.qq.com"
- mail_user="your qq number"
- mail_pass="your qq password"
- mail_postfix="qq.com"
- queue=Queue.Queue()
- class send_mail(threading.Thread):
- def __init__(self,threadname,queue):
- threading.Thread.__init__(self)
- self.threadname=threadname
- self.queue = queue
- self.start()
- def run(self):
- while True:
- if self.queue.empty():break
- to_list=self.queue.get()
- me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
- msg = MIMEText(content)
- msg['Subject'] = sub
- msg['From'] = me
- msg['To'] = to_list
- try:
- s = smtplib.SMTP()
- s.connect(mail_host)
- s.login(mail_user,mail_pass)
- s.sendmail(me, to_list, msg.as_string())
- s.close()
- print '发送成功:'+to_list
- self.queue.task_done()
- except Exception, e:
- print str(e)
- if __name__ == '__main__':
- #读取邮件列表
- file_object = open('list.txt')
- try:
- all_the_text = file_object.readlines( )
- finally:
- file_object.close( )
- mailto_list = all_the_text
- # 产生线程序列
- for i in mailto_list:
- print i
- queue.put(i)
- for i in range(10):
- threadname='Thread'+str(i)
- send_mail(threadname,queue)
- queue.join()
复制代码 |
|