码迷,mamicode.com
首页 > 编程语言 > 详细

python发送文本邮件

时间:2015-06-26 00:21:26      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

 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 = user 17 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 = 25 37 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())),returns 51 r.close()

 

python发送文本邮件

标签:

原文地址:http://www.cnblogs.com/caoguo/p/4601139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!