(简单有效,亲测可行)python实现每天准时发送邮件,每隔n秒发送邮件
2023-12-18 16:32:49
1.获取qq邮箱的授权码
https://jingyan.baidu.com/article/ac6a9a5eb439f36b653eacc0.html#:~:text=1%E3%80%81%E6%89%93%E5%BC%80%E3%80%90QQ%E5%AE%A2%E6%88%B7%E7%AB%AF%E3%80%91%EF%BC%8C%E8%BE%93%E5%85%A5%E3%80%90%E8%B4%A6%E5%8F%B7%E5%AF%86%E7%A0%81%E3%80%91%EF%BC%8C%E7%82%B9%E5%87%BB%E3%80%90%E5%AE%89%E5%85%A8%E7%99%BB%E9%99%86%E3%80%91%E3%80%82%202%E3%80%81%E5%B0%86%E9%BC%A0%E6%A0%87%E6%94%BE%E5%88%B0%E3%80%90%E5%A4%B4%E5%83%8F%E3%80%91%EF%BC%8C%E5%BC%B9%E5%87%BA%E6%93%8D%E4%BD%9C%E6%A1%86%EF%BC%8C%E7%82%B9%E5%87%BB%E3%80%90QQ%E9%82%AE%E7%AE%B1%E3%80%91%E3%80%82,3%E3%80%81%E8%BF%9B%E5%85%A5%E3%80%90QQ%E9%82%AE%E7%AE%B1%E3%80%91%EF%BC%8C%E7%82%B9%E5%87%BB%E4%B8%8A%E9%9D%A2%E3%80%90%E8%AE%BE%E7%BD%AE%E3%80%91%E3%80%82%204%E3%80%81%E5%9C%A8%E3%80%90%E9%82%AE%E4%BB%B6%E8%AE%BE%E7%BD%AE%E3%80%91%E9%A1%B5%E9%9D%A2%EF%BC%8C%E7%82%B9%E5%87%BB%E3%80%90%E8%B4%A6%E6%88%B7%E3%80%91%E3%80%82%205%E3%80%81%E9%BC%A0%E6%A0%87%E5%BE%80%E4%B8%8B%E6%8B%89%EF%BC%8C%E6%89%BE%E5%88%B0%E3%80%90POP3%2FIMAP%2FSMTP%2FExchange%2FCardDAV%2FCalDAV%E6%9C%8D%E5%8A%A1%E3%80%91%EF%BC%8C%E7%82%B9%E5%87%BB%E3%80%90%E7%94%9F%E6%88%90%E6%8E%88%E6%9D%83%E7%A0%81%E3%80%91%E3%80%82
2.填好自己的信息然后在个人电脑或者服务器运行如下python代码
pip install schedule
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import schedule
import time
def send_email(from_email,qq_password,to_email,subject,body):
# 配置QQ邮箱
from_email = from_email
qq_password = qq_password # 不是你的QQ密码,需要单独获取
smtp_server = "smtp.qq.com"
port = 587
# 邮件内容
subject = subject
body = body
# 配置邮件
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
try:
# 发送邮件
with smtplib.SMTP(smtp_server, port) as server:
server.starttls()
server.login(from_email, qq_password)
server.sendmail(from_email, [to_email], msg.as_string())
print('邮件发送成功')
except smtplib.SMTPException as e:
print(e)
print('邮件发送失败')
def send_email_at_fix_time(send_time,from_email,qq_password,to_email,subject,body):
schedule.every().day.at(send_time).do(send_email, from_email=from_email, qq_password=qq_password, to_email=to_email,
subject=subject, body=body)
while (True):
schedule.run_pending()
time.sleep(1)
def send_i_times_every_n_s(i,n,times,from_email,qq_password,to_email,subject,body):
i = i
while(i<n):
time.sleep(times) # 1h = 60 * 60s = 3600s, 22点运行 9个小时 6点 = 32400
send_email(from_email,qq_password,to_email,subject,body)
i=i+1
if __name__ == "__main__":
qq_password = "自己的授权码" # 不是你的QQ密码,需要单独获取
from_email = "自己的邮箱"
to_email = "收件人邮箱"
subject = "邮件标题"
body = "邮件内容"
send_time = "6:30" # 24小时制的时间
# 发送方式一: 固定时间发邮件
send_email_at_fix_time(send_time,from_email,qq_password,to_email,subject,body)
# 发送方式二,每隔n秒发送一次
i = 1
n=2
times = 60 # 每隔60s发送一次
send_i_times_every_n_s(i,n,times,from_email,qq_password,to_email,subject,body)
文章来源:https://blog.csdn.net/qq_42864343/article/details/135065071
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!