【发布时间】:2018-01-19 01:47:37
【问题描述】:
我希望构建一个 Python 工具来监控 Outlook 中的类别和文件夹更改。
到目前为止,我设法使用以下代码连接到 OnNewMailEx 事件并监视所有传入的电子邮件:
import win32com.client
import pythoncom
import re
def getPath(folder, path=[]):
if folder.parent.parent.parent:
path.append(folder.name)
getPath(folder.parent, path)
return "\\".join(reversed(path))
class Handler_Class(object):
def OnNewMailEx(self, receivedItemsIDs):
for ID in receivedItemsIDs.split(","):
mailItem = outlook.Session.GetItemFromID(ID)
if re.search("(TS)|(ST)", mailItem.Parent.FolderPath) != None:
print "Subj: " + mailItem.Subject
print "Time: " + str(mailItem.ReceivedTime)
print "Parent: " + str(mailItem.Parent.FolderPath)
# print "Body: " + mailItem.Body.encode( 'ascii', 'ignore' )
print "========"
outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class)
pythoncom.PumpMessages()
现在我正在尝试扩展它以连接到监视类别更改的事件。 MSDN 有这个主题:https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.olkcategory.aspx
但是,当我尝试下面的代码(使用来自 http://svn.cy55.de/changeset/1896?format=diff&new=1896 的正确名称)时,什么也没有发生:
class Handler_Class(object):
def OnChange(self):
print("Hook successful!")
category = win32com.client.DispatchWithEvents("Outlook.OlkCategoryStrip", Handler_Class)
pythoncom.PumpMessages()
此外,我找不到任何有关监控电子邮件是否移动到其他文件夹的事件的文档。有什么想法吗??
【问题讨论】:
标签: python outlook activex win32com dispatch