>>>>> gganesh <ganesh....@gmail.com> (g) wrote: >g> hi, >g> I'm a beginner in using Python script >g> I'm trying to send mails using multi-thread >g> I wrote
>g> FROM = 'ganeshx...@xxxx.com' >g> # for more mail add';' the another mail id >g> listTo = ['g....@gmail.com', 'xxjango...@gmail.com', >g> 'xx...@yahoo.co.in'] >g> SUBJECT = 'This is the subject' >g> MSGBODY = 'This the body of the message ' >g> MAILSERVER = 'mail.xxxx.com' >g> port = 25 >g> username = 'xxxxx' >g> password = 'xxxxx' >g> # trim the strings of any leading or trailing spaces >g> FROM = FROM.strip() >g> SUBJECT = SUBJECT.strip() >g> MSGBODY = MSGBODY.strip() >g> MAILSERVER = MAILSERVER.strip() >g> username = username.strip() >g> password = password.strip() >g> #Connect to server >g> print 'Connecting to mail server ', MAILSERVER >g> try: >g> s = smtplib.SMTP(MAILSERVER,port) You can't have a single SMTP connection that's used in multiple threads. That is what causes the error. Each thread should have its own SMTP connection. So move this code (above and below) into the run() method. >g> print 'connected' >g> #s.set_debuglevel(1) >g> except: >g> print 'ERROR: Unable to connect to mail server', sys.exc_info ()[0] >g> sys.exit(1) >g> #login to server >g> if password <> '': >g> print 'Logging into mail server' I think this try except block should be inside the if statement, i.e. indented 4 spaces. >g> try: >g> s.login(username,password) >g> except: >g> print 'ERROR: Unable to login to mail server', MAILSERVER >g> print 'Please recheck your password' >g> sys.exit(1) >g> #-------------------------------------------------- >g> print "Starting Multi Thread Method" >g> class MyThread(Thread): >g> def __init__(self, site, s, FROM, MSGBODY): >g> Thread.__init__(self) >g> self.site = site >g> self.s=s >g> self.FROM=FROM >g> self.MSGBODY=MSGBODY You give the s (connection) here as a parameter, store it in self.s and never use that attribute. >g> def run(self): >g> print "running for %s " %self.site >g> s.sendmail(self.FROM, self.site, self.MSGBODY) Here you use the global s, not self.s As I said above you should do the SMTP connection setup, including the login, here, and store the connection either in self.s or in a local variable s in the method. As you don't use the s in another method, I think a local variable is better. >g> print "Emailed for site %s" %self.site >g> a= time.time() >g> threads = [] >g> for site in listTo: >g> T = MyThread(site,s,FROM,MSGBODY) >g> threads.append(T) >g> T.start() >g> for i in threads: >g> i.join() Of course the next 2 lines should also be moved to run(). >g> s.quit() >g> s.close() >g> print "Took %s seconds" %str(time.time()-a) >g> #----------------------------------------------------- -- Piet van Oostrum <p...@cs.uu.nl> URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: p...@vanoostrum.org -- http://mail.python.org/mailman/listinfo/python-list