如何使用win32com和/或active_directory按名称访问邮件文件夹?

3 投票
4 回答
12382 浏览
提问于 2025-04-15 17:54

在使用Python和Outlook 2007时,结合win32com和active_directory,我该如何获取一个子文件夹的引用,以便将邮件移动到这个子文件夹里呢?

我的收件箱结构是这样的:

    Inbox
      |
      +-- test
      |
      `-- todo

我可以这样访问收件箱文件夹:

import win32com.client
import active_directory
session = win32com.client.gencache.EnsureDispatch("MAPI.session")
win32com.client.gencache.EnsureDispatch("Outlook.Application")
outlook = win32com.client.Dispatch("Outlook.Application")
mapi = outlook.GetNamespace('MAPI')
inbox =  mapi.GetDefaultFolder(win32com.client.constants.olFolderInbox)
print '\n'.join(dir(inbox))

但是当我试图按照微软的示例来获取名为test的子目录时,发现inbox对象没有Folders这个接口,也没有任何方法可以获取子目录。

那么我该如何获取一个指向test子目录的Folder对象呢?

4 个回答

1

这个方法对我来说有效,可以把邮件项目移动到一个叫“test”的子目录里(为了简单起见,去掉了一些复杂的缓存内容):

import win32com.client

olFolderInbox = 6
olMailItem = 0

outlook = win32com.client.Dispatch("Outlook.Application")
mapi = outlook.GetNamespace('MAPI')
inbox =  mapi.GetDefaultFolder(olFolderInbox)

item = outlook.CreateItem(olMailItem)
item.Subject = "test"

test_folder = inbox.Folders("test")
item.Move(test_folder)
2

对我有效的方法是逐个查看文件夹的名字。(当我发这个问题的时候,我还搞不清楚文件夹的名字)。

import win32com.client
import active_directory
session = win32com.client.gencache.EnsureDispatch("MAPI.session")
win32com.client.gencache.EnsureDispatch("Outlook.Application")
outlook = win32com.client.Dispatch("Outlook.Application")
mapi = outlook.GetNamespace('MAPI')
inbox =  mapi.GetDefaultFolder(win32com.client.constants.olFolderInbox)

fldr_iterator = inbox.Folders   
desired_folder = None
while 1:
    f = fldr_iterator.GetNext()
    if not f: break
    if f.Name == 'test':
        print 'found "test" dir'
        desired_folder = f
        break

print desired_folder.Name
6

我知道这个问题已经很老了,但最近我在使用win32com这个包时,发现它的文档实在是让人头疼……我希望将来能有人避免我在理解MSDN的解释时经历的那些折磨。

这里有一个Python脚本的例子,可以用来浏览Outlook文件夹,随意访问电子邮件。

免责声明:我对代码做了一些调整,去掉了一些敏感信息,所以如果你想直接复制粘贴运行,祝你好运。

import win32com
import win32com.client
import string
import os
# the findFolder function takes the folder you're looking for as folderName,
# and tries to find it with the MAPIFolder object searchIn

def findFolder(folderName,searchIn):
try:
    lowerAccount = searchIn.Folders
    for x in lowerAccount:
        if x.Name == folderName:
            print 'found it %s'%x.Name
            objective = x
            return objective
    return None
except Exception as error:
    print "Looks like we had an issue accessing the searchIn object"
    print (error)
    return None

def main():

outlook=win32com.client.Dispatch("Outlook.Application")

ons = outlook.GetNamespace("MAPI")

#this is the initial object you're accessing, IE if you want to access
#the account the Inbox belongs too
one = '<your account name here>@<your domain>.com'

#Retrieves a MAPIFolder object for your account 
#Object functions and properties defined by MSDN at 
#https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.mapifolder_members(v=office.14).aspx
Folder1 = findFolder(one,ons)

#Now pass you're MAPIFolder object to the same function along with the folder you're searching for
Folder2 = findFolder('Inbox',Folder1)

#Rinse and repeat until you have an object for the folder you're interested in
Folder3 = findFolder(<your inbox subfolder>,Folder2)

#This call returns a list of mailItem objects refering to all of the mailitems(messages) in the specified MAPIFolder
messages = Folder3.Items

#Iterate through the messages contained within our subfolder
for xx in messages:
    try:
        #Treat xx as a singular object, you can print the body, sender, cc's and pretty much every aspect of an e-mail
       #In my case I was writing the body to .txt files to parse...
        print xx.Subject,xx.Sender,xx.Body
       #Using move you can move e-mails around programatically, make sure to pass it a 
        #MAPIFolder object as the destination, use findFolder() to get the object
        xx.Move(Folder3)


    except Exception as err:
        print "Error accessing mailItem"
        print err       


if __name__ == "__main__":
main()

PS 希望这不会带来更多麻烦。

撰写回答