使用Python在Windows Outlook中保存电子邮件时出现com_error
我正在使用Python将电子邮件保存在指定的文件夹中,并完成以下内容:
- 检查电子邮件是否未阅读
- 将电子邮件保存到目标文件夹中,
- 将消息标记为读取,
- 请告诉我归档了多少条消息,并且代码的位置
:
from win32com.client import Dispatch
import os
outlook = Dispatch('outlook.application').GetNamespace("MAPI")
root = outlook.Folders[0].Folders[27]
targetPath = os.path.expanduser(r'\\path\to\target\folder')
messages = root.Items
count = 0
for msg in messages:
if msg.UnRead:
msg.SaveAs(os.path.join(targetPath))
msg.UnRead = False
count +=1
print(str(count)+' emails archived to: '+targetPath)
我最终出现有了我以前从未见过的错误,我对Google机器没有任何运气...所以我来stackoverflow并从沉睡中唤醒您。
Traceback (most recent call last):
File "C:\Users\e582324\Documents\Python Scripts\untitled6.py", line 18, in <module>
msg.SaveAs(os.path.join(targetPath))
File "<COMObject <unknown>>", line 3, in SaveAs
com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The operation failed.', None, 0, -2147024829), None)
知道这是什么原因引起的?我有一个类似的脚本,可以节省附件,而且效果很好。我的目标文件夹在两个脚本的网络共享驱动器上,直到现在还没有引起问题。唯一真正的区别是使用.saveas()而不是.saveasfile()
I'm using python to save emails in a specified folder and complete the following:
- check if the email is unread
- save the email to target folder
- mark the message as read
- tell me how many messages were archived and the location
the code:
from win32com.client import Dispatch
import os
outlook = Dispatch('outlook.application').GetNamespace("MAPI")
root = outlook.Folders[0].Folders[27]
targetPath = os.path.expanduser(r'\\path\to\target\folder')
messages = root.Items
count = 0
for msg in messages:
if msg.UnRead:
msg.SaveAs(os.path.join(targetPath))
msg.UnRead = False
count +=1
print(str(count)+' emails archived to: '+targetPath)
I end up with this error i've never seen before, and I haven't had any luck with the google machine... so I come to stackoverflow and wake you from your slumber.
Traceback (most recent call last):
File "C:\Users\e582324\Documents\Python Scripts\untitled6.py", line 18, in <module>
msg.SaveAs(os.path.join(targetPath))
File "<COMObject <unknown>>", line 3, in SaveAs
com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The operation failed.', None, 0, -2147024829), None)
Any idea whats causing this? I have a similar script that saves just attachments and it works fine. My target folder is on a network shared drive for both scripts and it hasn't caused an issue until now. Only real difference is the use of .SaveAs() instead of .SaveAsFile()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要指定文件名及其扩展名。有时传递路径是不够的,特别是当您不指定格式时(请参阅第二个参数)。请注意,Outlook 文件夹可能包含不同类型的项目 - 邮件项目、约会、文档、注释等。例如,这里是一个示例:
此外,我注意到您正在迭代文件夹中的所有项目:
相反,您需要使用
Items
类的Find
/FindNext
或Restrict
方法。因此,您只能获得与您的条件相对应的项目,并且仅对它们进行迭代。在以下文章中了解有关这些方法的更多信息:You need to specify the file name as well as its extension. passing the path is not enough sometimes, especially when you don't specify the format (see the second parameter). Note, an Outlook folder may contain different kind of items - mail items, appointments, documents, notes and etc. For example, here is a sample:
Also I've noticed that you are iterating over all items in the folder:
Instead, you need to use the
Find
/FindNext
orRestrict
methods of theItems
class. So, you get only items that correspond to your conditions and iterate only over them. Read more about these methods in the following articles: