[python-win32] Exchange Server Event Solution

Johann Abraham johann.abraham at gmail.com
Tue Jul 11 21:15:57 CEST 2006


I wanted to post the solution to a question in Feb. of 2002 because it took
me a little bit longer to solve than I thought it would and I thought if
I could spare people having to rummage through the exchange SDK on MSDN I
could also save them a lot of time.  The question was how to access an
exchange server event but a lot of the details were left out, including how
to register the events with exchange.

Step 1)  Use the PythonWin Com MakePy utility to for EXOLEDB

Step 2) Make your Com class:

ie.


from win32com.client import Dispatch
from win32com import universal

IExStoreSyncEvents_methods  = ["OnSyncSave"]
universal.RegisterInterfaces('{GUID for EXOLEDB MakePy file}',0,1,0,
["IExStoreSyncEvents"]) #GUID in file name


class MyEventListener:
        _public_methods_ = IExStoreSyncEvents_methods
        _com_interfaces_ = ['IExStoreSyncEvents']   #interface to implement


        _reg_clsid_      = "{D7CDBFB7-7B49-4EA4-9F8E-47152ED86991}"  #CLSID
unique
        _reg_progid_     = "MyEventListener"
        _reg_desc_       = "My Event Listener"

        # IExStoreSyncEvents interfaces
        def OnSyncSave(self, pEventInfo, strUrl, lFlags):
#pEventInfo,strUrl,IFlags <- check SDK
                # Called by a store when an item is saved.
                self.sendMessage('ToUser at Domain.com','FromUser at kelsan.com',
'tester','OnSyncSave') #tests that sync works properly
                return


        def OnSyncDelete(self, pEventInfo, strUrl, lFlags):
                # Called by a store when an item is deleted.
                pass

        def sendMessage(self, To, From, Subject, Text): #sends email
            Msg = Dispatch("CDO.Message")

            Msg.To = To
            Msg.From = From
            Msg.Subject = Subject
            Msg.TextBody = Text

            #' Send the message.
            Msg.Send()

            return

if __name__=='__main__':
    try:
        # import event sink type library
        import win32com.server.register
        win32com.server.register.UseCommandLine(MyEventListener)
    except:
        raise RuntimeError, "could not import event sink type library"

Step 3) Make a COM+ object

Programs->Administrative tools->Component Services

under Component Services tab, select: computers->My Computer->Com+
Applications

right click Com+ Applications

Select New->Application

click next->Create An Empty Application

Name application same as class

Select Server Application

Click next

Define the scope for the com+ class, most likely you'll want it for a single
user and want it to run regardless if someone is logged on so:  Select: this
user->browse, type name and Check names, click OK

fill in password, click next

click next, next, and finish

expand Com+ Applications tree, expand your new com+ object tree

right click Components->new->Component

next->Import Component->select your Com class->finish

select Com+ Application, Right click, select properties, find Security tab,

turn off Enforce Access Checks for this application

select Perform access checks only at process level. Select Okay

Step 4) Open Command Prompt

find regevent.vbs, should be in C:\Program Files\Exchange
SDK\SDK\Support\OLEDB\Scripts

C:\Program Files\Exchange SDK\SDK\Support\OLEDB\Scripts>cscript
RegEvent.vbsadd onsyncsave MyEventListener
file://./backofficestorage/Domain/mbx/user/inbox/evtreg1

Now send some mail to whichever inbox you set up the event at, give it a few
minutes, and enjoy that you didn't have to read the MSDN Exchange SDK yet!
Alas, the code isn't perfect and you'll receive two emails instead of one,
thats what the IFlags are for in the OnSyncSave function, check out the VB
or C++ code given in the SDK and you should have that whipped pretty fast.

Johann
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20060711/7d62ca35/attachment.htm 


More information about the Python-win32 mailing list