首页 > 学院 > 开发设计 > 正文

python发送文本邮件

2019-11-14 17:08:33
字体:
来源:转载
供稿:网友
 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()

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表