JasonYeung 2020-06-07
#实现邮件发送import smtplib#email实现邮件的构建from email.mime.text import MIMETextfrom email.header import Headerimport schedule,timedef job(): # 如何实现文本邮件发送 message = MIMEText(_text="正在发送邮件", _subtype=‘plain‘, _charset=‘utf-8‘) message["From"] = Header("", "utf-8") message["To"] = Header("", "utf-8") message["Subject"] = Header("python email send test", "utf-8") smtpobj = smtplib.SMTP() try: # smtp服务器地址,连接smtp服务器 mail_host = "smtp.qq.com" smtpobj.connect(host=mail_host, port="587") # 用户登录,用户名为发送者地址,密码不是账号的密码,是授权码 # 如何去获取授权码 # 发送者邮件点击设置-账户-开启pop3/smtp协议 获取授权码 user = "" smtpobj.login(user=user, password="授权码") # 实现邮件的发送 sender = "" receiver = [""] smtpobj.sendmail(sender, receiver, message.as_string()) print("邮件发送成功") except smtplib.SMTPException as e: print("邮件发送失败", e)schedule.every().day.at("21:35").do(job) # 部署在每天的10:30执行job()函数的任务# schedule.every().monday.do(job) # 部署每个星期一执行job()函数的任务# schedule.every().wednesday.at("13:15").do(job) # 部署每周三的13:15执行函数的任务# schedule.every(1).seconds.do(send_message)# schedule.every(10).minutes.do(job) # 部署每10分钟执行一次job()函数的任务# schedule.every().hour.do(job) # 部署每×小时执行一次job()函数的任务flag = Truewhile flag: schedule.run_pending() time.sleep(1)