testExchange.py :  » Windows » pyExcelerator » pywin32-214 » com » win32com » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » com » win32com » test » testExchange.py
# TestExchange = Exchange Server Dump
# Note that this code uses "CDO", which is unlikely to get the best choice.
# You should use the Outlook object model, or
# the win32com.mapi examples for a low-level interface.

from win32com.client import gencache,constants
import pythoncom
import os

ammodule = None # was the generated module!

def GetDefaultProfileName():
    import win32api, win32con
    try:
        key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows Messaging Subsystem\\Profiles")
        try:
            return win32api.RegQueryValueEx(key, "DefaultProfile")[0]
        finally:
            key.Close()
    except win32api.error:
        return None

#
# Recursive dump of folders.
#
def DumpFolder(folder, indent = 0):
    print " " * indent, folder.Name
    folders = folder.Folders
    folder = folders.GetFirst()
    while folder:
        DumpFolder(folder, indent+1)
        folder = folders.GetNext()

def DumpFolders(session):
    try:
        infostores = session.InfoStores
    except AttributeError:
        # later outlook?
        store = session.DefaultStore
        folder = store.GetRootFolder()
        DumpFolder(folder)
        return

    print infostores
    print "There are %d infostores" % infostores.Count
    for i in range(infostores.Count):
        infostore = infostores[i+1]
        print "Infostore = ", infostore.Name
        try:
            folder = infostore.RootFolder
        except pythoncom.com_error, details:
            hr, msg, exc, arg = details
            # -2147221219 == MAPI_E_FAILONEPROVIDER - a single provider temporarily not available.
            if exc and exc[-1]==-2147221219:
                print "This info store is currently not available"
                continue
        DumpFolder(folder)

# Build a dictionary of property tags, so I can reverse look-up
#
PropTagsById={}
if ammodule:
    for name, val in ammodule.constants.__dict__.iteritems():
        PropTagsById[val] = name


def TestAddress(session):
#       entry = session.GetAddressEntry("Skip")
#       print entry
    pass


def TestUser(session):
    ae = session.CurrentUser
    fields = getattr(ae, "Fields", [])
    print "User has %d fields" % len(fields)
    for f in range(len(fields)):
        field = fields[f+1]
        try:
            id = PropTagsById[field.ID]
        except KeyError:
            id = field.ID
        print "%s/%s=%s" % (field.Name, id, field.Value)

def test():
    import win32com.client
    oldcwd = os.getcwd()
    try:
        session = gencache.EnsureDispatch("MAPI.Session")
        try:
            session.Logon(GetDefaultProfileName())
        except pythoncom.com_error, details:
            print "Could not log on to MAPI:", details
            return
    except pythoncom.error:
        # no mapi.session - let's try outlook
        app = gencache.EnsureDispatch("Outlook.Application")
        session = app.Session

    try:
        TestUser(session)
        TestAddress(session)
        DumpFolders(session)
    finally:
        session.Logoff()
        # It appears Exchange will change the cwd on us :(
        os.chdir(oldcwd)

if __name__=='__main__':
    from util import CheckClean
    test()
    CheckClean()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.