原文:http://blog.sina.com.cn/s/blog_3fe961ae01013rz3.html
最近在实验室的服务器上做一些测试,因为测试时间经常要好几个小时甚至十几个小时,所以我想到在测试结束后将结果发送到我的邮箱中,这样就可以及时地了解到测试的结果是什么而不用守在计算机前。 整理了Python发送邮件的相关代码如下: send_email.py
#coding=utf8 def send_email_163(from_addr = 'xxxxxx@163.com',password = 'xxxxxx', to_addrs = ('xxxxxx@163.com'), subject = 'TestRusult', content = None ): """This function use 163 email to send simple message.If success,return Ture,else return False from_addr:should be 163 email adress password:password of your email account to_addrs:should be a tuple,like ('xxxxxx@163.com','xxxxxx@163.com') subject:subject of your email content:content of your email """ if content is None: print 'content is None.' return False try: from smtplib import SMTP from email.mime.text import MIMEText email_client = SMTP(host = 'smtp.163.com') email_client.login(from_addr, password) #create msg msg = MIMEText(content, _charset = 'utf-8') msg['Subject'] = subject email_client.sendmail(from_addr,to_addrs, msg.as_string()) return True except Exception,e: print e return False finally: email_client.quit()
test.py
#coding=utf8 if __name__ == '__main__':from send_email import send_email_163 print send_email_163(content = '阿涵')
上述代码经测试,可用。