使用python发送电子邮件

#!c:/Python25/python.exe
import smtplib
from email.mime.text import MIMEText

mailto_list=["[email protected]","[email protected]"]

mail_host="smtp.163.com"
mail_user="user"
mail_pass="password"
mail_postfix="163.com"

def send_mail(to_list,sub,content):
    print "start----"+sub+" "+content
    me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
    print "me----"+me
    msg = MIMEText(content)
    msg['Subject'] = sub
    msg['From'] = me
    msg['To'] = ";".join(to_list)
    print "msg---"+msg.as_string()
    try:
        s = smtplib.SMTP()
        print "smtp---"
        s.connect(mail_host)
        print "connect---"
        s.login(mail_user,mail_pass)
        print "login--"
        s.sendmail(me, to_list, msg.as_string())
        s.close()
        return True
    except Exception, e:
        print str(e)
        return False
if __name__== '__main__':
    print "hello"
    if send_mail(mailto_list,"subject","content"):
        print "succ"
    else:
        print "fail"

你可能感兴趣的:(使用python发送电子邮件)