您所在的位置:小祥子 » 编程 » Python » 正文

python发送文本邮件

时间:2015-06-26 编辑:Ca0Gu0 来源:本站整理
 #!/usr/bin/env python
 #coding=utf-8
 #Author: Ca0Gu0
import time import smtplib from email.mime.text import MIMEText class MailCli(object): def __init__(self): self.s = smtplib.SMTP() #类实例化 def connect(self, host=None, port=25): self.s.connect(host, port) #连接邮件服务器 def login(self, user=None, passwd=None): self.user = user self.s.login(user, passwd) #登陆帐户跟密码 def send(self, subject=None, content=None, to=None): froms = self.user+'<%s>' %(self.user) msg = MIMEText(content) #处理发件内容 msg['Subject'] = subject #处理发件主题 msg['From'] = froms #处理发件人地址 msg['To'] = to #处理收件人地址 self.s.sendmail(froms, to, msg.as_string()) #发送邮件内容 return "OK" def close(self): self.s.close() #退出登陆 return '0' #显示发信成功 if __name__ == "__main__": host = "mail.xxx.com" port = 25 user = "caoguo@xxx.com" passwd = "password" to = "caoguo@163.com" r=MailCli() r.connect(host=host,port=port) r.login(user=user, passwd=passwd) for i in range(10): #连续发生10封邮件 subject = "Testing SMTP Authentication "+ str(i) content = "This mail tests SMTP Authentication" + str(i) returns = r.send(subject=subject, content=content, to=to) print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())),returns r.close()
关键词:文本