[python-win32] how to create a instance of PyIMAPISession by hand

Mark Hammond skippy.hammond at gmail.com
Wed May 8 15:59:15 CEST 2013


On 8/05/2013 11:29 PM, Christian K. wrote:
>
> The following code has been suggested to me to get a handle to Outlook's
> mapi session.
>
>
> from win32com.client import Dispatch
> from win32com.mapi import mapi
> app = Dispatch('Outlook.Application')
> app.GetNamespace('MAPI').Session.MAPIOBJECT
>
> The MAPIOBJECT would then have to be cast to a IMAPISession object. How
> would I do that? By calling the PyIMAPISesison constructor with MAPIOBJECT
> as argument? Unfortunately I was not able to find the PyIMAPISession class
> in the pywin32 namespace nor when digging through the source.
>
> <PyIUnknown at 0x020D0F20 with obj at 0x003A53DC>

It should be something like:

from win32com.mapi import mapi
session = 
app.GetNamespace('MAPI').Session.MAPIOBJECT.QueryInterface(mapi.IID_IMAPISession)

Note that a good resource for working with MAPI from pywin32 is the 
spambayes project, and particularly:

https://github.com/smontanaro/spambayes/blob/master/spambayes/Outlook2000/msgstore.py

For reasons that escape me now, it does things differently - after 
creating the Outlook.app object, it does:

         mapi.MAPIInitialize(None)
         logonFlags = (mapi.MAPI_NO_MAIL |
                       mapi.MAPI_EXTENDED |
                       mapi.MAPI_USE_DEFAULT)
         self.session = mapi.MAPILogonEx(0, None, None, logonFlags)

to get the session.  Note however that that code was written against 
Office 2000, so some things - especially work-arounds etc - may well 
have changed

HTH,

Mark



More information about the python-win32 mailing list