python - 在 python 脚本中使用 Pywin32 和 outlook 发送自动电子邮件是有效的,但是当通过 Windows 任务计划程序自动发送电子邮件时不起作用

标签 python outlook pywin32

我写了一个 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/

相关文章:

vba - 当多个项目一次添加到 Outlook 文件夹时,如何触发事件?

anaconda - PyInstaller win32ctypes.pywin32.pywintypes.error : (2, 'LoadLibraryExW' , 'The system cannot find the file specified.')

python - 使用 pywin32 在登录屏幕中无法使用键盘

python - 2016 年 Django 服务器发送的事件

python - 如何使用 python-instagram 接收实时 Instagram 更新?

python - Pyside2 QQuickView没有rootContext?

python - Linux下的pywin32模块是什么?

Python CSV Reader 似乎按句点分隔

Java 邮件附件未在 Outlook 中显示

c# - Office VSTO 加载项可能的权限问题 - HRESULT 0x80004004 (E_ABORT)