Python 中是否有任何东西可以持续检查是否收到带有特定主题标题的 Outlook 电子邮件?

Is there anything in Python that can persistently check to see if an Outlook email with a certain subject title came in?

我想知道 Python 中是否有任何东西可以每五分钟持续读取我的 Outlook 邮件。我希望它每 5 分钟(下午 3:05、3:10PM、3:15PM 等)检查我的电子邮件,并且一旦我的收件箱中收到一封主题为“你好,怎么了up" 我想让Python自动触发一段.py代码。

目前我所知道的

我已经知道如何访问 Outlook -

import win32com.client
win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
inbox = mapi.GetDefaultFolder(1)
...
...

一些可能性

我目前正在查看几个链接,例如 , , how to trigger a script upon email receipt as well as a Youtube video that talks about how to run a python script upon sending an email.

希望我能够在收到电子邮件后弄清楚如何 运行 我的 Python 脚本,但与此同时这个问题仍然悬而未决。提前致谢。

NewMailEx event of the Outlook Application class fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID 方法和处理项目。

NewMailEx 事件处理程序中,您可能会收到一个项目实例并检查主题 属性。

您可以处理收件箱中的未读电子邮件(参见 Items.RestrictItems.Find/FindNext)假设新的未处理邮件仍未读或(在缓存模式的情况下) ) 在收件箱文件夹上使用 Items.ItemAdd 事件 - 它会在您的 OST 文件与远程邮箱同步时触发。

感谢您的帮助。已经一个月了,但我重新审视了这个问题并找到了解决方案。虽然我找不到可以侦听 Outlook 电子邮件的 python 程序,但 Outlook 中的 VBA 可以侦听电子邮件,然后启动 python 脚本。

我的 python 代码最初用于直接编辑 Excel 电子表格。因此,例如,当我收到一封带有特定主题行 blah 的电子邮件时,python 文件 main.py 将自动执行并相应地编辑 excel 文件 main.xlsx

需要注意的是 main.xlsx 在电子邮件到达时无法打开。如果有人对如何修改 main.xlsx 在电子邮件打开时到达的机会有任何指导,我将不胜感激。

下面的代码完全解决了我的问题:

Private Sub inboxItems_ItemAdd(ByVal Item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim MessageInfo
Dim Result
If TypeName(Item) = "MailItem" Then
    Debug.Print "Arrived"
    If Item.Subject = "Blah" Then
        Const PyExe = """C:\...\...\...\...\...\python3.9\latest\python.exe"""
        Const PyScript = "R:\...\...\Anders\Python\main.py"
        
        Dim objShell As Object, cmd As String
        Set objShell = CreateObject("Wscript.Shell")
        
        cmd = PyExe & " " & PyScript
        Debug.Print cmd
        
        objShell.Run cmd
        objShell.exec cmd
        
        MsgBox objShell.exec(cmd).StdOut.ReadAll
    End If
End If
ExitNewItem:
    Exit Sub
ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ExitNewItem
End Sub