#!/usr/bin/env python # -*- coding: utf-8 -*- #导入smtplib和MIMEText import smtplib from email.MIMEText import MIMEText mail_to="[email protected]" #要发给谁 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 "发送失败"
可以使用,但是为了防止你的邮箱被退回,或是作为垃圾邮件,要每隔一段时间在发送。
时间: 2024-10-05 07:55:46