1 #!/usr/bin/env python2 #Coding=utf-83#Author: ca0gu0
4 Import Time5 ImportSmtplib6 fromEmail.mime.textImportMimetext7 8 classMailcli (object):9 def __init__(self):TenSelf.s = Smtplib. SMTP ()#class instantiation One A defConnect (self, host=none, port=25): -Self.s.connect (host, Port)#connecting to a mail server - the defLogin (self, user=none, passwd=None): -Self.user =User -Self.s.login (user, passwd)#login account and password - + - defSend (self, subject=none, Content=none, to=None): +Froms = self.user+'<%s>'%(Self.user) Amsg = Mimetext (content)#working with outgoing content atmsg['Subject'] = subject#working with outgoing senders topics -msg[' from'] = Froms#working with sender addresses -msg[' to'] = To#Handling Recipient addresses -Self.s.sendmail (Froms, To, msg.as_string ())#Send Message content - return "OK" - in defClose (self): -Self.s.close ()#Exit Login to return '0' #Show successful sending + - the if __name__=="__main__": *Host ="mail.xxx.com" $Port = 25Panax Notoginsenguser ="[email protected]" -passwd ="Password" theto ="[email protected]" + AR=mailcli () theR.connect (host=host,port=Port) +R.login (User=user, passwd=passwd) - $ forIinchRange (10): #连续发生10封邮件 $ -Subject ="Testing SMTP Authentication"+Str (i) -Content ="This mail tests SMTP authentication"+Str (i) thereturns = R.send (Subject=subject, Content=content, to=to ) - PrintTime.strftime ("%y-%m-%d%h:%m:%s", Time.localtime (Time.time ())), returnsWuyiR.close ()
Python sends text messages