Has anyone had success using the PyIMAPIFolder CopyMessages method?
The spambayes source looks as tho' it's using it happily, so I can't
imagine there's any fundamental problem. Only... it doesn't work for
me. :(
I have a reproducible test case attached which is -- in the way of the
extended MAPI code -- a bit wordy. But I believe it is self-contained
and could be run by anyone with an Exchange account and a folder called
"Docs". The code is also on pastebin: http://pastebin.com/m575adec6
In short, I find my Inbox and my Docs folder and attempt to copy an
arbitrary message from the one to the other. The code completes without
error, but nothing seems to be happening. There's nothing to suggest
that any "commit"-type action is necessary. Just in case, I've tried
various combinations of SaveChanges to no avail. I look in the Docs
folder on Outlook and nothing's there.
All suggestions gratefully received.
TJG
from win32com.mapi import mapi, mapitags
LOGON_FLAGS = mapi.MAPI_NO_MAIL | mapi.MAPI_UNICODE | mapi.MAPI_EXTENDED |
mapi.MAPI_USE_DEFAULT
mapi.MAPIInitialize ((mapi.MAPI_INIT_VERSION, 0))
try:
session = mapi.MAPILogonEx (0, "", None, LOGON_FLAGS)
try:
#
# Find the default message store (my Mailbox)
#
message_stores = session.GetMsgStoresTable (0)
filter = mapi.RES_PROPERTY, (mapi.RELOP_EQ, mapitags.PR_DEFAULT_STORE,
(mapitags.PR_DEFAULT_STORE, True))
mailbox = None
for rows in mapi.HrQueryAllRows (message_stores, (mapitags.PR_ENTRYID,),
filter, None, 0):
for tag, store_id in rows:
mailbox = session.OpenMsgStore (0, store_id, None,
mapi.MAPI_BEST_ACCESS)
break
if not mailbox:
raise RuntimeError ("Can't find mailbox")
#
# Find my inbox
#
inbox_id, _ = mailbox.GetReceiveFolder (None, 0)
inbox = mailbox.OpenEntry (inbox_id, None, 0)
#
# Find my Docs folder
#
n_props, props = mailbox.GetProps ([mapitags.PR_IPM_SUBTREE_ENTRYID])
for tag, root_folder_id in props:
root_folder = mailbox.OpenEntry (root_folder_id, None, 0)
break
else:
raise RuntimeError ("root folder not found")
hierarchy = root_folder.GetHierarchyTable (0)
for item in mapi.HrQueryAllRows (hierarchy, None, None, None, 0):
tags = dict (item)
if tags[mapitags.PR_DISPLAY_NAME_A] == "Docs":
docs_folder = mailbox.OpenEntry (tags[mapitags.PR_ENTRYID], None, 0)
break
else:
raise RuntimeError ("Docs not found")
#
# Pick up a random message from the inbox
#
inbox_contents = inbox.GetContentsTable (0)
for item in mapi.HrQueryAllRows (inbox_contents, None, None, None, 0):
tags = dict (item)
print tags[mapitags.PR_SUBJECT_A]
message_id = tags[mapitags.PR_ENTRYID]
break
#
# Now:
# inbox - PyIMAPITable
# docs_folder - PyIMAPITable
# message_id - Entry ID of a specific message in the Inbox
#
result = inbox.CopyMessages (
[message_id],
None,
docs_folder,
0,
None,
0
)
finally:
session.Logoff (0, 0, 0)
finally:
mapi.MAPIUninitialize ()
_______________________________________________
python-win32 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-win32