RSS Home Newsletter Advertising
Visit Twellow.com

Removing Incoming Email in MS Exchange, C# Example

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 http://www.codeproject.com/csharp/CsManagedEventSinksHooks.asp , 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) {
&nbsp&nbsp&nbsp try {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbspif (IFlags == ((int)EVT_SINK_FLAGS.EVT_SYNC_COMMITTED + (int)EVT_SINK_FLAGS.EVT_IS_DELIVERED)) {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp ProcessMessage(pEventInfo, bstrURLItem, IFlags);
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp }
&nbsp&nbsp&nbsp }
&nbsp&nbsp&nbsp catch (Exception ex) {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp log.Debug(ex.Message + "\n" + ex.StackTrace);
&nbsp&nbsp&nbsp }
&nbsp&nbsp&nbsp finally {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp LogManager.Shutdown();
&nbsp&nbsp&nbsp }
}

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.apache.org/log4net/

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 {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp&nbsp ADODB.Connection oCn = new ADODB.Connection();

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp oCn.Provider = "exoledb.datasource";
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp oCn.Open(bstrURLItem, "", "", -1);

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp if(oCn.State == 1) {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp log.Debug("Good Connection");
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp }
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp else {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp log.Debug("Bad Connection");
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp }

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

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp rec.Open(bstrURLItem, oCn,
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp ADODB.ConnectModeEnum.adModeReadWrite,
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp ADODB.RecordCreateOptionsEnum.adFailIfNotExists,
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp ADODB.RecordOpenOptionsEnum.adOpenSource,
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp "", "");

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp rec.DeleteRecord(bstrURLItem, false);
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp rec.Close();

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp oCn.Close();

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp rec = null;
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp oCn = null;
&nbsp&nbsp&nbsp&nbsp }
&nbsp&nbsp&nbsp&nbsp catch (Exception ex) {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp log.Debug(ex.Message + "\n" + ex.StackTrace);
&nbsp&nbsp&nbsp&nbsp }
}

Happy customizing!

Boris Makushkin is senior software developer in Alba Spectrum Technologies US nation-wide Great Plains, Microsoft CRM customization company, based in Chicago and having locations in multiple states and internationally (www.albaspectrum.com ), he is Unix, SQL, C#.Net, Crystal Reports, Microsoft CRM SDK and Exchange Server SDK developer. You can reach Boris: borism@albaspectrum.com

Digg This! StumbleUpon This!


News Tags: Email, HTTP
About the author:
Boris Makushkin is senior software developer in Alba Spectrum Technologies US nation-wide Great Plains, Microsoft CRM customization company, based in Chicago and having locations in multiple states and internationally (www.albaspectrum.com ), he is Unix, SQL, C#.Net, Crystal Reports, Microsoft CRM SDK and Exchange Server SDK developer. You can reach Boris: borism@albaspectrum.com

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
6 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.