如何使用 python win32com 访问默认文件夹以外的 outlook 文件夹(如收件箱、已发送)?

How to access the outlook folders other than default ones (like Inbox, Sent) using python win32com?

这是我访问收件箱的方式:

   outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
   inbox = outlook.GetDefaultFolder("6")

当我尝试使用以下代码访问用户在 Outlook 中创建的文件夹时:

   outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
   Folder = outlook.Folders[1]
   print (Folder)

我收到这个错误:

  raise IndexError("list index out of range")

IndexError: list index out of range

如有任何帮助,我们将不胜感激。

在全球范围内,您可以:

from win32com.client import Dispatch
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
root_folder = outlook.Folders.Item(1)

然后你可以通过

查看这个文件夹的名称
print (root_folder.Name)

并了解您拥有的子文件夹的名称:

for folder in root_folder.Folders:
    print (folder.Name)

最后,假设您想访问 root_folder 中名为 folder_of_soldy 的子文件夹,您可以:

soldy_folder = root_folder.Folders['folder_of_soldy']

如果您在 folder_of_soldy 中还有其他子文件夹,则以此类推。

希望你找到你需要的东西