我目前正在使用pywin32.com python库将邮件移动到outlook中的特定文件夹。 我必须将文件夹路径设置为可配置的,因此我创建了一个python文件,将文件夹路径作为列表保存在其中。FoldersConfig.py文件的内容是:
#相对于收件箱文件夹的文件夹名称
mailBox = "mailbox-name"
nagiosDestinationFolder = ["Resolved_Clients","Internal","Nagios alerts"] #all these folders are been created in outlook with exact names.
然后还有另一个文件将邮件移动到目标文件夹。就我而言,Nagios alerts是我的目标文件夹。MoveNagiosAlerts.py文件的代码段为:
outlook = client.Dispatch("Outlook.Application").GetNamespace("MAPI")
stores = outlook.Stores
for store in stores:
mailBox = FoldersConfig.mailBox
if(mailBox in store.DisplayName):
rootFolder = store.GetDefaultFolder(6) #rootFolder is the Inbox folder
break
def moveWarnings():
print("Moving warning mails...")
warnCount = 0
scriptingDictionary = {}
nagiosDestFolder = rootFolder
for i in FoldersConfig.nagiosDestinationFolder:
nagiosDestFolder = nagiosDestFolder.Folders(i)
print(nagiosDestFolder)
for i in range(rootFolder.Items.Count-1,0,-1):
msg = rootFolder.Items[i]
if(msg.Body.find("State: WARNING") != -1):
msg.Move(nagiosDestFolder)
warnCount+=1
return warnCount
运行MoveNagiosAlerts.py文件时弹出的错误是:
Root folder(Inbox): Inbox
Moving warning mails..
Traceback (most recent call last):
File "C:\Users\Documents\MoveNagiosAlerts.py", line 193, in <module>
main()
File "C:\Users\Documents\MoveNagiosAlerts.py", line 172, in main
warningsCount = moveWarnings()
File "C:\Users\Documents\MoveNagiosAlerts.py", line 56, in moveWarnings
nagiosDestFolder = nagiosDestFolder.Folders(i)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\win32com\client\dynamic.py", line 181, in __call__
return self._get_good_object_(self._oleobj_.Invoke(*allArgs),self._olerepr_.defaultDispatchName,None)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The attempted operation failed. An object could not be found.', None, 0, -2147221233), None)
Outlook folder structure for your refrence
我的问题是为什么会抛出此错误??它有时工作正常,但大多数情况下会抛出此错误。你知道我的代码到底出了什么问题吗??还是outlook的问题?
根据我的理解,我认为pywin客户端无法获取列表中的文件夹。但不确定这是否是原因
我注意到代码中有以下循环:
移动项目时,集合中的项目数将减少。所以,你需要为此做好准备
另外,如果只想移动与条件相对应的项,则需要使用
Find
/FindNext
或Restrict
方法来获取这些项,并在不检查任何其他属性的情况下移动所有项。请在以下文章中阅读有关这些方法的更多信息:相关问题 更多 >
编程相关推荐