使用win32com和/或active_directory,我怎么能使用名称来访问电子邮件文件夹? [英] Using win32com and/or active_directory, how can I access an email folder by name?

查看:1723
本文介绍了使用win32com和/或active_directory,我怎么能使用名称来访问电子邮件文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在蟒蛇瓦特/ Outlook 2007中,使用win32com和/或active_directory,我怎么能得到一个引用的子文件夹,这样我可以移动的MailItem这个子文件夹?

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

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

我可以访问收件箱文件夹,如:

 进口win32com.client
进口active_directory
会议= win32com.client.gencache.EnsureDispatch(MAPI.session)
win32com.client.gencache.EnsureDispatch(Outlook.Application)
展望= win32com.client.Dispatch(Outlook.Application)
MAPI = outlook.GetNamespace(MAPI)
收件箱= mapi.GetDefaultFolder(win32com.client.constants.olFolderInbox)
打印'\ n'.join(DIR(收件箱))
 

但是,当我试图让子目录测试元的微软的例子中的收件箱对象不具备文件夹接口或任何的方式来获得一个子目录。

我如何获得文件夹目标指向测试子目录?

解决方案

我意识到这是一个老问题,但我一直在使用win32com包最近,发现麻烦的文件,至少可以说...我的希望是,一个人,一天可以保存在动荡,我经历试图总结我的头周围MSDN的解释

下面是和例如Python脚本来遍历Outlook文件夹,访问电子邮件,我请。

免责声明的 我转向周围的code和拿出一些敏感的信息,所以如果你想复制和粘贴,并让它运行,好运气。

 进口win32com
进口win32com.client
进口字符串
进口OS
#在findFolder函数有你要找的是夹folderName的文件夹,
#并试图找到它与MAPIFolder对象searchIn

高清findFolder(夹folderName,searchIn):
尝试:
    lowerAccount = searchIn.Folders
    对于x在lowerAccount:
        如果x.Name ==夹folderName:
            打印发现%s'的%x.Name
            客观= X
            返回目标
    返回None
除了异常的错误:
    打印看来我们​​有访问searchIn对象的问题
    打印(错误)
    返回None

高清的main():

展望= win32com.client.Dispatch(Outlook.Application)

附件= outlook.GetNamespace(MAPI)

#这个是你所访问,IE浏览器最初的对象,如果您要访问
#系统帐户的收件箱所属的太
1 ='<在这里您的帐户名称>&@ LT;您的域> .COM

#Retrieves一个MAPIFolder对象为您的帐户
MSDN通过在定义#Object的功能和性能
#https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.mapifolder_members(v=office.14).aspx
文件夹1 = findFolder(一,附件)

#Now通过你MAPIFolder对象相同的功能,以及您要搜索的文件夹
FOLDER2 = findFolder(收件箱,Folder1中)

#Rinse并重复,直到你有一个对象对你感兴趣的文件夹
Folder3 = findFolder(<您的收件箱中的子文件夹>,FOLDER2)

#这个调用返回指定MAPIFolder的MailItem对象指的是所有的mailitems(消息)的列表
消息= Folder3.Items

#Iterate通过包含我们的子文件夹中的邮件
为XX的消息:
    尝试:
        #Treat XX作为一个单一的对象,你可以打印体,发件人,消委会和pretty的的邮件太多各个方面
       #In我来说,我在写体为.txt文件解析...
        打印xx.Subject,xx.Sender,xx.Body
       #使用移动您可以将电子邮件编程左右,一定要传递一个
        #MAPIFolder对象作为目标,使用findFolder()来获取对象
        xx.Move(Folder3)


    除了异常的错误:
        打印错误访问的MailItem
        打印犯错


如果__name__ ==__main__:
主要()
 

PS希望这不会做弊大于利。

In python w/ Outlook 2007, using win32com and/or active_directory, how can I get a reference to a sub-folder so that I may move a MailItem to this sub-folder?

I have an inbox structure like:

    Inbox
      |
      +-- test
      |
      `-- todo

I can access the inbox folder like:

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

But when I try to get subdirectory test per Microsoft's example the inbox object doesn't have the Folders interface or any way to get a subdirectory.

How can I get a Folder object which points to test subdir?

解决方案

I realize this is an old question but I've been using the win32com package recently and found the documentation troublesome to say the least...My hope is that someone, someday can be saved the turmoil I experienced trying to wrap my head around MSDN's explanation

Here's and example of a python script to traverse through Outlook folders, accessing e-mails where I please.

Disclaimer I shifted around the code and took out some sensitive info so if you're trying to copy and paste it and have it run, good luck.

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 Hope this doesn't do more harm than good.

这篇关于使用win32com和/或active_directory,我怎么能使用名称来访问电子邮件文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆