帮我写一个QQ邮箱定时发送内容或附件的python代码
时间: 2023-02-12 07:25:43 浏览: 303
python实现定时发送qq消息
5星 · 资源好评率100%
下面是一个示例代码,使用 smtplib 库发送 QQ 邮件,需要提前开启 QQ 邮箱的 SMTP 服务,并获取授权码。
```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from datetime import datetime, timedelta
import schedule
import time
def send_email(to_addr, subject, body, attachments=[]):
from_addr = "你的QQ邮箱地址"
password = "授权码"
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = subject
msg.attach(MIMEText(body))
for attachment in attachments:
with open(attachment, "rb") as f:
part = MIMEApplication(f.read(), _subtype="octet-stream")
part.add_header("Content-Disposition", "attachment", filename=attachment)
msg.attach(part)
try:
server = smtplib.SMTP_SSL("smtp.qq.com", 465)
server.login(from_addr, password)
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()
print("邮件发送成功")
except smtplib.SMTPException as e:
print("Error: 无法发送邮件", e)
def job():
to_addr = "收件人邮箱地址"
subject = "邮件主题"
body = "邮件内容"
attachments = ["附件1路径", "附件2路径"]
send_email(to_addr, subject, body, attachments)
def main():
schedule.every().day.at("08:00").do(job)
while True:
schedule.run_pending()
time.sleep(1)
if __name__ == '__main__':
main()
```
这段代码会在每天的08:00发送邮件,邮件主题,内容,附件在代码中自行设置。
阅读全文