Removing Incoming Email in MS Exchange, C# Example


Visitors: 2,510

The purpose of one of our projects was MS Exchange handler for processing incoming email.

The basic source of knowledge was this article “Developing Managed Event Sinks/Hooks for Exchange Server Store using C#" by Logu Krishnan, published to the address , and also examples from Microsoft Exchange SDK.

We utilized Synchronous Events and created the handler, which fires on OnSyncSave event. The handler creates activity record in Microsoft CRM and then removes the message in the Exchange database before the commitment:

public void OnSyncSave(IExStoreEventInfo pEventInfo, string bstrURLItem, int IFlags) { try { if (IFlags = ((int)EVT_SINK_FLAGS. EVT_SYNC_COMMITTED + (int)EVT_SINK_FLAGS. EVT_IS_DELIVERED)) {

ProcessMessage(pEventInfo, bstrURLItem, IFlags);



catch (Exception ex) {

log. Debug(ex. Message + “n" + ex. StackTrace);


finally {

LogManager. Shutdown();

} }

For Exchange handlers debugging - it is the extremely convenient to use system log4net in RollingLogFileAppender or RemoteAppender modes (for multiple instance of COM + objects). You can read more on this subject here http://logging. To allow the handler incoming mail removal, it is necessary to give proper rights to the user, under which account the COM+ application runs the handler. These are rights on change of the information in user’s boxes for whom it is registered (Windows 2003 Server: Active Directory Users and Computer -> Users -> Properties (for COM+ application account) -> Exchange Advanced -> Mailbox Rights). And now the code:

private void DeleteMessage(string bstrURLItem) { try {

ADODB. Connection oCn = new ADODB. Connection();

oCn. Provider = “exoledb. datasource";

oCn. Open(bstrURLItem, “", “", -1);

if(oCn. State = 1) {

log. Debug("Good Connection");


else {

log. Debug("Bad Connection");


ADODB. Record rec = new ADODB. Record();

rec. Open(bstrURLItem, oCn,

ADODB. ConnectModeEnum. adModeReadWrite,

ADODB. RecordCreateOptionsEnum. adFailIfNotExists,

ADODB. RecordOpenOptionsEnum. adOpenSource,

"", “");

rec. DeleteRecord(bstrURLItem, false);

rec. Close();

oCn. Close();

rec = null;

oCn = null;


catch (Exception ex) {

log. Debug(ex. Message + “n" + ex. StackTrace);

} }

Happy customizing! Boris Makushkin

Boris Makushkin is Software Engineer in Alba Spectrum Technologies – USA nationwide Microsoft CRM, Microsoft Great Plains customization company, based in Chicago, Boston, San Francisco, San Diego, Los Angeles, Houston, Atlanta, New York, and Miami and having locations in multiple states and internationally ( ), he is Dexterity, SQL, VB/C#. Net, Crystal Reports and Microsoft CRM SDK developer.


Article Source:

Rate this Article: 
Creative Ways to Get Quality Incoming Links
Rated 4 / 5
based on 5 votes

Related Articles:

Removing Malware Some Quick Tips on Removing Malware Out of Your System ..

by: Jim Marshall (July 29, 2008) 
(Computers and Technology/Software)

Get Targeted Hits to Your Website Using Traffic Exchange and Banner Exchange ..

by: Sarpras Navas (October 19, 2005) 
(Internet and Businesses Online/Traffic Building)

Foreign Exchange Swaps - Profiting As The Exchange Rate Moves Up And Down

by: Donald Saunders (March 11, 2007) 

How To Handle Incoming Leads

by: Mike Brooks (July 18, 2007) 
(Business/Sales Teleselling)

How to Use Tracking to Keep Income Incoming

by: Ron Hutton (August 19, 2005) 
(Internet and Businesses Online/Internet Marketing)

Organizing Your Incoming Paper Mail

by: Heidi Woods (May 11, 2008) 
(Self Improvement/Organizing)

So You Want To Build Up Your Reciprocal And Incoming Links?

by: Rachel Gawith (July 03, 2006) 
(Internet and Businesses Online/Site Promotion)

10 Ways To Get Incoming Links To Your Website

by: James Woolley (July 21, 2006) 
(Internet and Businesses Online/Traffic Building)

Foreign Exchange Rate - Foreign Currency Exchange - Foreign Exchange Student

by: Eddie Yakubovich (February 23, 2007) 

Creative Ways to Get Quality Incoming Links

by: Maksym Nesen (December 02, 2005) 
(Internet and Businesses Online/Traffic Building)