【问题标题】:Using win32com and/or active_directory, how can I access an email folder by name?使用 win32com 和/或 active_directory,如何按名称访问电子邮件文件夹?
【发布时间】:2010-01-11 18:34:38
【问题描述】:

在带有 Outlook 2007 的 python 中,使用 win32com 和/或 active_directory,我如何获取对子文件夹的引用,以便可以将 MailItem 移动到该子文件夹?

我的收件箱结构如下:

收件箱 | +-- 测试 | `--待办事项

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

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))

但是当我尝试通过Microsoft's example 获取子目录test 时,inbox 对象没有Folders 接口或任何获取子目录的方式。

如何获得指向test 子目录的Folder 对象?

【问题讨论】:

    标签: python active-directory outlook win32com


    【解决方案1】:

    我意识到这是一个老问题,但我最近一直在使用 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 希望这不会弊大于利。

    【讨论】:

    • 谢谢托尼。是的。我发现处理这个问题也很痛苦。
    【解决方案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
    

    【讨论】:

      【解决方案3】:

      这对我来说可以将邮件项移动到“测试”子目录中(通过摆脱 gencache 的东西来简化):

      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)
      

      【讨论】:

      • 这个电话对我不起作用inbox.Folders('test')。但是我确实发现我可以迭代 inbox.Folders 并针对 a_folder.Name 进行测试
      • 另外,您可以在 win32com 中找到所有这些 Outlook 常量 -- win32com.client.constants.olFolderInbox
      【解决方案4】:

      所以下面的代码将抓取 SOURCE 文件夹中的“Last”项,然后将其移动到 DEST 文件夹。抱歉代码有点生硬,我删除了所有附加功能,例如阅读和保存邮件。

      import win32com.client
      
      inbox = win32com.client.gencache.EnsureDispatch("Outlook.Application").GetNamespace("MAPI")
      
      
      source = inbox.GetDefaultFolder(6).Folders["FOLDER_NAME_SOURCE"]
      dest = inbox.GetDefaultFolder(6).Folders["FOLDER_NAME_DEST"]
      
      
      def moveMail(message):
          print("moving mail to done folder")
          message.Move(dest)
          return print("MOVED")
      
      
      def getMail():
          message = source.Items.GetLast()
          moveMail(message)
      
      
      getMail()
      

      【讨论】:

        猜你喜欢
        • 2011-12-23
        • 1970-01-01
        • 1970-01-01
        • 2022-08-22
        • 2015-12-20
        • 1970-01-01
        • 2017-06-17
        • 2020-07-04
        • 1970-01-01
        相关资源
        最近更新 更多