testExchange.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # TestExchange = Exchange Server Dump
  2. # Note that this code uses "CDO", which is unlikely to get the best choice.
  3. # You should use the Outlook object model, or
  4. # the win32com.mapi examples for a low-level interface.
  5. from win32com.client import gencache, constants
  6. import pythoncom
  7. import os
  8. ammodule = None # was the generated module!
  9. def GetDefaultProfileName():
  10. import win32api, win32con
  11. try:
  12. key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows Messaging Subsystem\\Profiles")
  13. try:
  14. return win32api.RegQueryValueEx(key, "DefaultProfile")[0]
  15. finally:
  16. key.Close()
  17. except win32api.error:
  18. return None
  19. #
  20. # Recursive dump of folders.
  21. #
  22. def DumpFolder(folder, indent = 0):
  23. print(" " * indent, folder.Name)
  24. folders = folder.Folders
  25. folder = folders.GetFirst()
  26. while folder:
  27. DumpFolder(folder, indent+1)
  28. folder = folders.GetNext()
  29. def DumpFolders(session):
  30. try:
  31. infostores = session.InfoStores
  32. except AttributeError:
  33. # later outlook?
  34. store = session.DefaultStore
  35. folder = store.GetRootFolder()
  36. DumpFolder(folder)
  37. return
  38. print(infostores)
  39. print("There are %d infostores" % infostores.Count)
  40. for i in range(infostores.Count):
  41. infostore = infostores[i+1]
  42. print("Infostore = ", infostore.Name)
  43. try:
  44. folder = infostore.RootFolder
  45. except pythoncom.com_error as details:
  46. hr, msg, exc, arg = details
  47. # -2147221219 == MAPI_E_FAILONEPROVIDER - a single provider temporarily not available.
  48. if exc and exc[-1]==-2147221219:
  49. print("This info store is currently not available")
  50. continue
  51. DumpFolder(folder)
  52. # Build a dictionary of property tags, so I can reverse look-up
  53. #
  54. PropTagsById={}
  55. if ammodule:
  56. for name, val in ammodule.constants.__dict__.items():
  57. PropTagsById[val] = name
  58. def TestAddress(session):
  59. # entry = session.GetAddressEntry("Skip")
  60. # print entry
  61. pass
  62. def TestUser(session):
  63. ae = session.CurrentUser
  64. fields = getattr(ae, "Fields", [])
  65. print("User has %d fields" % len(fields))
  66. for f in range(len(fields)):
  67. field = fields[f+1]
  68. try:
  69. id = PropTagsById[field.ID]
  70. except KeyError:
  71. id = field.ID
  72. print("%s/%s=%s" % (field.Name, id, field.Value))
  73. def test():
  74. import win32com.client
  75. oldcwd = os.getcwd()
  76. try:
  77. session = gencache.EnsureDispatch("MAPI.Session")
  78. try:
  79. session.Logon(GetDefaultProfileName())
  80. except pythoncom.com_error as details:
  81. print("Could not log on to MAPI:", details)
  82. return
  83. except pythoncom.error:
  84. # no mapi.session - let's try outlook
  85. app = gencache.EnsureDispatch("Outlook.Application")
  86. session = app.Session
  87. try:
  88. TestUser(session)
  89. TestAddress(session)
  90. DumpFolders(session)
  91. finally:
  92. session.Logoff()
  93. # It appears Exchange will change the cwd on us :(
  94. os.chdir(oldcwd)
  95. if __name__=='__main__':
  96. from .util import CheckClean
  97. test()
  98. CheckClean()