使用python win32com迭代子文件夹的Outlook

时间:2016-11-28 17:05:47

标签: python outlook win32com

我有以下代码可以获取我的共享文件夹的收件箱,以及里面的所有电子邮件。此代码效果很好,将打印上一封电子邮件的主题。

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
recip = outlook.CreateRecipient("foo@bar.com")
inbox = outlook.GetSharedDefaultFolder(recip, 6)
messages = inbox.Items
message = messages.GetLast()
print (message.Subject)

我可以访问foo@bar.com邮箱中的其他父文件夹(例如已发送),但我无法获取收件箱内任何文件夹的子文件夹,或者更深入。所以,如果我想要inbox \ subfolder1,我该如何访问?如果重要,请使用Outlook 2013。 我的主要目标是:

message.Move(inbox\subfolder1)

7 个答案:

答案 0 :(得分:10)

是的,最好把它写成文件夹的名称,而不是写文件夹编号

与我的文件夹层次结构类似:Outlook_Mails>收件箱>重要

outlook = win32.com.client.Dispatch("Outlook.Application")
mapi = outlook.GetNamespace("MAPI")

your_folder = mapi.Folders['Outlook_Mails'].Folders['Inbox'].Folders['Important']
for message in your_folder.Items:
    print(message.Subject)

答案 1 :(得分:5)

这是我用来执行类似任务的代码。

outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace("MAPI")
root_folder = namespace.Folders.Item(1)
subfolder = root_folder.Folders['All'].Folders['Main Folder'].Folders['Subfolder']
messages = subfolder.Items

这会在文件夹"所有/主文件夹/子文件夹"中找到消息。

答案 2 :(得分:3)

无法执行此操作 - Outlook在主OST文件中缓存共享默认文件夹不缓存子文件夹。如果相关邮箱被添加为代理商店,您应该能够使用Namespace.FoldersNamespace.Stores解析相关文件夹。

否则您可以使用Redemption及其RDOSessionGetSharedDefaultFolder - 该文件夹将以在线模式打开,包含所有子文件夹(RDOFolder。{{1 }})。

答案 3 :(得分:0)

特斯拉爵士。 实际上,我跟进了你的代码模式&根据我当前的项目改变它。 请找到以下标本代码。

import win32com.client
outlook = win32com.client.Dispatch("Outlook.application")
mapi = outlook.GetNamespace("MAPI")
FirstFMB = mapi.Folders['FirstFMB'].Folders['Inbox']
SecondFMB = mapi.Folders['SecondFMB'].Folders['Another_folder']

<Hence other loops & operations as per requirement>

我在这里知道了一件事。当我们需要使用某种功能邮箱执行时,我们只需要将名称放在 mapi.Folder [] 下,然后继续使用流程。

特斯拉爵士您的代码模式对我有用,而不是使用文件夹编号

另一方面,这种技术可以帮助我附加邮件阅读和阅读。在一定时间内采取行动。

答案 4 :(得分:0)

def processfolder(folder):

    ignoredfolders = []
    
    if not folder.Name in ignoredfolders:
        print("processing", folder.Name)
        count=0
        for mail in folder.Items:
               savemsg(mail)
               count += 1

        print(count, "Mails in folder")
        for fld in folder.Folders:
            processfolder(fld)

答案 5 :(得分:-1)

我接受了Jared Goguen的回答并对其进行了修改。

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
root_folder = inbox.Folders(6)
messages = root_folder.Items

inbox.Folders(6)使用我感兴趣的子文件夹的索引来识别它。我能够使用此消息成功迭代子文件夹中的消息。

答案 6 :(得分:-1)

import win32com.client as win32

# new outlook object
outlook = win32.Dispatch("Outlook.Application")

# get user namespace *Important when reading email*
namespace = outlook.GetNamespace("MAPI")

# Default inbox folder either Folders.Item(1/2)
root_folder = namespace.Folders.Item(2)

# Use this function to display subfolders inside the current folder

def menu(outlookFolderItem):
    for i in range(0,20):
        try:
            print(i,outlookFolderItem.Folders(i).Name)
        except:
            pass

# example
menu(root_folder)

# navigate into the subfile by

sub_folder = root_folder.Folders(2).Folders(14)