[python-win32] Extended MAPI : ConfigureMsgService Problem
Whitaker, Simon SITI-SITI
Simon.Whitaker at shell.com
Wed Jan 19 05:49:13 CET 2005
Here you go - a more complete version.
Script creates a new profile, adds and configures a personal and private store and then lists all of the top level folders.
Comments welcome, but be gentle with me - I'm still learning!
---
from win32com.mapi import mapi, mapiutil, mapitags
import pythoncom
import re
profileName = "Test"
# Initialise
mapi.MAPIInitialize(None)
#mapi.MAPIInitialize((mapi.MAPI_INIT_VERSION, mapi.MAPI_NT_SERVICE))
# Get the handle to administer the profiles
profileAdmin = mapi.MAPIAdminProfiles(0)
# Get the current profiles
profileTable=profileAdmin.GetProfileTable(0)
profileRows = mapi.HrQueryAllRows(profileTable, [mapitags.PR_DISPLAY_NAME_A], None, None, 0)
# Delete the profile if it already exists
for profile in profileRows:
if profile[0][1] == profileName:
profileAdmin.DeleteProfile(profileName, 0)
break
# Create the profile
profileAdmin.CreateProfile(profileName, None, 0, 0)
# Administer the profile services
serviceAdmin = profileAdmin.AdminServices(profileName, None, 0, 0)
serviceAdmin.CreateMsgService('MSEMS', None, 0, 0) # Add an Exchange service
serviceAdmin.CreateMsgService('MSPST MS', None, 0, 0) # Add a .pst file
# Get the service table - looking for service IDs
msgServiceTable = serviceAdmin.GetMsgServiceTable(0)
msgServiceRows = mapi.HrQueryAllRows(msgServiceTable, [mapitags.PR_SERVICE_UID], None, None, 0)
# Get the service ID of the MSEMS service (first)
serviceUID = msgServiceRows[0][0][1]
serviceUID = pythoncom.MakeIID(serviceUID, 1)
# Configure the Exchange Service
propsTuple = ((mapitags.PR_PROFILE_UNRESOLVED_NAME, "USER"),(mapitags.PR_PROFILE_UNRESOLVED_SERVER, "SERVER"))
serviceAdmin.ConfigureMsgService(serviceUID, 0, 0, propsTuple)
# Get the service ID of the MS PST service (last)
serviceUID = msgServiceRows[-1][0][1]
serviceUID = pythoncom.MakeIID(serviceUID, 1)
# Configure the .pst file
PR_PST_PATH = int(0x6700001E) # This tag is not defined in mapitags?
propsTuple = ((mapitags.PR_DISPLAY_NAME_A, "Temp"), (PR_PST_PATH, r"c:\temp.pst"))
serviceAdmin.ConfigureMsgService(serviceUID, 0, 0, propsTuple)
# Now logon to the profile
session = mapi.MAPILogonEx(0, profileName, None, mapi.MAPI_EXTENDED | mapi.MAPI_NEW_SESSION | mapi.MAPI_NO_MAIL)
#session = mapi.MAPILogonEx(0, profileName, None, mapi.MAPI_EXTENDED | mapi.MAPI_NEW_SESSION | mapi.MAPI_NO_MAIL | mapi.MAPI_NT_SERVICE)
# Get the EMS, PF and PST store IDs
msgStoresTable = session.GetMsgStoresTable(0)
propTags = [mapitags.PR_PROVIDER_DISPLAY_A, mapitags.PR_DISPLAY_NAME_A, mapitags.PR_ENTRYID]
msgStoresRows = mapi.HrQueryAllRows(msgStoresTable, propTags, None, None, 0)
# Now iterate through each store and print out the top level folder names
for msgStore in msgStoresRows:
msgStoreID = msgStore[2][1]
msgStoreName = msgStore[1][1]
if (msgStore[0][1] == "Microsoft Exchange Server" or "Microsoft Exchange Message Store") and re.search("^Mailbox", msgStore[1][1]):
msgStoreType = "private"
subtreeEIDTag = mapitags.PR_IPM_SUBTREE_ENTRYID
elif (msgStore[0][1] == "Microsoft Exchange Server" or "Microsoft Exchange Message Store") and msgStore[1][1] == "Public Folders":
msgStoreType = "public"
subtreeEIDTag = mapitags.PR_IPM_PUBLIC_FOLDERS_ENTRYID
elif msgStore[0][1] == "Personal Folders" and re.search("^Temp", msgStore[1][1]):
msgStoreType = "personal"
subtreeEIDTag = mapitags.PR_IPM_SUBTREE_ENTRYID
#Use your MAPI session to call the IMAPISession::OpenMsgStore method.
msgStore = session.OpenMsgStore(0, msgStoreID, None, mapi.MDB_NO_DIALOG | mapi.MAPI_BEST_ACCESS)
#Use the resulting message database pointer to call the IMAPIProp::GetProps method for the PR_IPM_SUBTREE_ENTRYID property.
hr, props = msgStore.GetProps((subtreeEIDTag,), 0)
subtreeEID = props[0][1]
#Call the IMsgStore::OpenEntry method with the entry identifier to get an IMAPIFolder pointer.
subtreeFolder = msgStore.OpenEntry(subtreeEID, None, 0)
#Call the IMAPIContainer::GetHierarchyTable method to get a table of the contents of the folder.
subtreeFolderHierarchy = subtreeFolder.GetHierarchyTable(0)
#Call the IMAPITable::QueryRows method to list the folders in the top-level folder.
subtreeFolderHierarchyRows = mapi.HrQueryAllRows(subtreeFolderHierarchy, [mapitags.PR_DISPLAY_NAME_A], None, None, 0)
# Print store name
print "\n" + msgStoreName + "\n"
# Print out the folder name
for row in subtreeFolderHierarchyRows:
foldername = "\t" + row[0][1]
print foldername
#servicemanager.LogInfoMsg(foldername)
# Uninitialise
mapi.MAPIUninitialize()
---
More information about the Python-win32
mailing list