我写了一个 python 脚本,它使用 win32com.client.Dispatch("Outlook.Application")
通过 outlook 发送自动电子邮件。
如果我自己运行脚本,一切都很好。但是,如果我通过 Window 的 task scheduler
运行它,它不会发送电子邮件。
只是为了检查我是否正确运行了脚本,我让脚本输出了一个随机文本文件,这有效,但电子邮件无效。为什么?
最佳答案
在发送电子邮件之前,您会在电子邮件中添加任何附件吗?我有一个类似的问题,但它现在工作得很好。如果我的脚本中有两个不同的函数来发送电子邮件(例如,一个在发生错误时发送电子邮件,另一个在脚本成功运行时发送电子邮件),我会从 Outlook 收到“操作中止”错误.这是因为在其中一个功能中我会在电子邮件中添加附件,但在另一个功能中我不会。不要问我为什么,但这会导致错误。为了解决这个问题,我只需要在不需要附件的电子邮件中添加一个冗余附件即可。
def emailComplete():
ol = DispatchEx("Outlook.Application")
Msg = ol.CreateItem(0)
Msg.To = "recip@ient.com"
Msg.Subject = "foo complete"
Msg.Attachments.Add("C:\Path\to\blank\attachment.txt") # send a blank attachment to stop the 'operation aborted' error
Msg.Send()
ol.Quit()
def emailError():
ol = DispatchEx("Outlook.Application")
Msg = ol.CreateItem(0)
Msg.To = "recip@ient.com"
Msg.Subject = "foo errored"
Msg.Attachments.Add("C:\path\to\error\file.txt") # send the error file with the email
Msg.Send()
ol.Quit()
这不是最优雅的解决方案,但它让我的工作正常!!
关于python - 在 python 脚本中使用 Pywin32 和 outlook 发送自动电子邮件是有效的,但是当通过 Windows 任务计划程序自动发送电子邮件时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24926733/