1 #!/usr/bin/env python 2 #coding=utf-8 3 #Author: Ca0Gu0
4 import time 5 import smtplib 6 from email.mime.text import MIMEText 7 8 class MailCli(object): 9 def __init__(self):10 self.s = smtplib.SMTP() #類實例化11 12 def connect(self, host=None, port=25):13 self.s.connect(host, port) #連接郵件服務器14 15 def login(self, user=None, passwd=None):16 self.user = user17 self.s.login(user, passwd) #登陸帳戶跟密碼18 19 20 def send(self, subject=None, content=None, to=None):21 froms = self.user+'<%s>' %(self.user)22 msg = MIMEText(content) #處理發件內容23 msg['Subject'] = subject #處理發件主題24 msg['From'] = froms #處理發件人地址25 msg['To'] = to #處理收件人地址 26 self.s.sendmail(froms, to, msg.as_string()) #發送郵件內容27 return "OK"28 29 def close(self):30 self.s.close() #退出登陸31 return '0' #顯示發信成功32 33 34 if __name__ == "__main__":35 host = "mail.xxx.com"36 port = 2537 user = "caoguo@xxx.com"38 passwd = "passWord"39 to = "caoguo@163.com"40 41 r=MailCli()42 r.connect(host=host,port=port)43 r.login(user=user, passwd=passwd)44 45 for i in range(10): #連續發生10封郵件46 47 subject = "Testing SMTP Authentication "+ str(i)48 content = "This mail tests SMTP Authentication" + str(i)49 returns = r.send(subject=subject, content=content, to=to)50 PRint time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())),returns51 r.close()
新聞熱點
疑難解答