【问题标题】:Cant send E-Mail with Windows Task Scheduler无法使用 Windows 任务计划程序发送电子邮件
【发布时间】:2021-02-18 08:54:08
【问题描述】:

我有 Task1 - 它在 pandas 中生成报告并将其保存为 excel 文件。然后我有 Task2,它找到保存的文件并通过 Outlook 将其发送给我的同事。 我正在使用 .bat 文件来制作作业,并使用 Windows 任务计划程序来运行它们。 问题是 - 当点击 .bat 文件时它会发送一封电子邮件,但是当我在调度程序上运行它时它不会发送一封电子邮件

我该如何解决这个问题?

Task2的代码:

import win32com.client as client
from datetime import datetime
import pathlib

outlook = client.Dispatch('Outlook.Application')
message = outlook.CreateItem(0)  # 0 is the code for a mail item (see the enumerations)
message.To = '@.com'
message.Subject = 'Report from ' + str(datetime.now().date())
message.Body = 'Report from ' + str(datetime.now().date())
path = pathlib.Path('C:\\Report ' \
                    + str(datetime.now()).split()[0] + '.xlsx')
absolute = str(path.absolute())
message.Attachments.Add(absolute)

message.Send()

【问题讨论】:

    标签: python outlook scheduled-tasks


    【解决方案1】:

    您可以在 python 中使用schedule 库。

    例如,您可以每天在hour 点运行一次脚本或函数function_name,例如:

    import schedule
    
    schedule.every().day.at(hour).do(function_name)
    
    while True:
         schedule.run_pending()  
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      相关资源
      最近更新 更多