Consider the following recommendations when designing a custom agent.
Be careful with security, because the MAPI session that you're passed has powerful permissions within Microsoft Exchange Server. Be particularly careful when handing control to another element in the system, especially if you are not the creator of that other element.
The EventSource object is created by the Exchange Event Handler and the IEventSource interface is passed to your object using your implementation of the IExchangeEventHandler::ExecuteEvent method. The IDL file and MIDL generated C++ header files are provided in the include directory of the Platform SDK. The names are exchhndl.h, exchhndl_i.c, and exchhndl.idl.
Do your own exception handling by watching out for exceptions and access violations.