【Python】Python使用smtplib发送简单文本邮件

原文: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 = '阿涵')
上述代码经测试,可用。

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值