首页 > 代码库 > 用python向邮箱发信息

用python向邮箱发信息

#!/usr/bin/env python# -*- coding: utf-8 -*-#导入smtplib和MIMETextimport smtplibfrom email.MIMEText import MIMETextmail_to="XXXXXXXX@qq.com"#要发给谁def send_mail(to_list,sub,content):    #设置服务器,用户名、口令以及邮箱的后缀    mail_host="smtp.163.com"    mail_user="你邮箱的账号"    mail_pass="你的密码"    mail_postfix="163.com"    me=mail_user+"<"+mail_user+"@"+mail_postfix+">"            msg = MIMEText(content)    msg[Subject] = sub    msg[From] = me    msg[To] = to_list    print msg    try:        s = smtplib.SMTP()        s.connect(mail_host)        s.login(mail_user,mail_pass)        s.sendmail(me, to_list, msg.as_string())        s.close()        print 1        return True    except Exception, e:        print 2        print str(e)        return False#if __name__ == ‘__main__‘:if __name__ == __main__:    if send_mail(mail_to,"hello","this is python sent"):        print "发送成功"    else:        print "发送失败"

可以使用,但是为了防止你的邮箱被退回,或是作为垃圾邮件,要每隔一段时间在发送。

用python向邮箱发信息