在outlook中使用python win32com模块触发新邮件的操作

svgewumm  于 2023-01-10  发布在  Python
关注(0)|答案(2)|浏览(266)

当新邮件到达outlook时,是否可以使用python模块win32com来触发某些操作
伪码

while 1
        if a mail arrives
            do x

编辑:不允许我使用“运行脚本”规则

uz75evzq

uz75evzq1#

import pythoncom
from win32com.client import DispatchWithEvents

# Event handler class for Outlook events
class OutlookEventHandler(object):
    @staticmethod
    def OnNewMailEx(EntryIDCollection):
        for ID in EntryIDCollection.split(","):
            item = Outlook.Session.GetItemFromID(ID)
            # check item class, 43 = MailItem
            if item.Class == 43:
                print(" Subj: " + item.Subject)

if __name__ == "__main__":
    Outlook = DispatchWithEvents("Outlook.Application", OutlookEventHandler)
    olNs = Outlook.GetNamespace("MAPI")
    Inbox = olNs.GetDefaultFolder(6)
    pythoncom.PumpMessages()
  • 一个月一次 *
  • 示例 *
import pythoncom
from win32com.client import DispatchWithEvents, Dispatch

# Event handler class for outlook events
class OutlookEvent(object):
    @staticmethod
    def OnItemAdd(item):
        """ Name    Required/Optional   Data type   Description
            Item    Required            Object      The item that was added."""
        print(f'The item that was added = {item.Subject}')

if __name__ == "__main__":
    outlook = Dispatch("outlook.Application")
    olNs = outlook.GetNamespace("MAPI")
    inbox = olNs.GetDefaultFolder(6)

    event = DispatchWithEvents(inbox.Items, OutlookEvent)
    pythoncom.PumpMessages()
rsl1atfo

rsl1atfo2#

您可以创建运行VBA脚本的规则。该脚本可以执行任何您希望它执行的操作,包括运行Python代码。

相关问题