python通过163邮箱发送邮件

发布时间:2019-07-24 09:48:33编辑:auto阅读(1537)

    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    import smtplib
    import sys
    import os
    import datetime
    import time
    import MySQLdb
    import subprocess
    
    
    
    
    reload(sys)
    sys.setdefaultencoding("utf-8")
    
    
    
    
    mailto_list = ["xxx@163.com"]  #目标邮箱
    mail_host = "smtp.163.com"    
    mail_user = "xxx@163.com"  
    mail_pass = "lecpuedugk"  #163邮箱smtp生成的密码
    
    
    
    def send_mail(to_list, sub, content):
        me = "LogServer"+"<"+mail_user+">"
        msg = MIMEText(content, _subtype='plain', _charset='utf-8')
        msg['Subject'] = sub    
        msg['From'] = me
        msg['To'] = ";".join(to_list)
        try:
            server = smtplib.SMTP()
            server.connect(mail_host)
            server.login(mail_user, mail_pass)
            server.sendmail(me, to_list, msg.as_string())
            server.close()
            return True
        except Exception, e:
            print str(e)
            return False
    
    
    if __name__ == '__main__':
            send_mail(mailto_list, 'submit', 'content')

关键字