C# Managed Event Sink -- how to use CDO to get Subject and Message Body

N

Notlwonk

I have written an event sink in C# that hooks into our Exchange Server
2000 store.

The sink fires and the various LOG files are created, but for some
reason I cannot seem to read-in the e-mail Subject or Message Body for
the e-mail that triggers the Event Sink to run.

What am I doing wrong?

Thank you,

Tom

==============================


using System;
using System.Runtime.InteropServices;
using System.Xml;
using CDO;
using ADODB;
using System.IO;
using System.Text;
using System.EnterpriseServices;



namespace MyEventSink
{
/// <summary>
/// Summary description for Class1.
/// </summary>
[Guid("16369924-5F32-4E26-AE89-8308B5C162E2")]

public class ExchEventSink: ServicedComponent , IExStoreAsyncEvents
{
//public string ClassID = "F92EFC3A-FDD8-4225-B005-13FD3D5D54D1";
//public string InterfaceId =
"704A413F-F8FE-476B-9206-69AB6300D752";
//public string EventsId = "DCC71BD5-6627-4FBF-BB5A-DB8FEA1EB177";

public string ClassID = "cf63289d-18d1-43af-ba90-9e41cac93ee6";
public string InterfaceId = "323733b0-3c0d-47d4-86fa-fd67d651cf81";
public string EventsId = "3721271c-65f0-441a-91d6-6a86d1c75846";


public ExchEventSink()
{


}

#region IExStoreAsyncEvents Members

public void OnSave(IExStoreEventInfo pEventInfo, string bstrURLItem,
int lFlags)
{
try
{
if(System.Convert.ToBoolean(lFlags))
{
Process_mail(pEventInfo, bstrURLItem);

FileStream fs = new
FileStream(@"c:\temp\ProofThatOnSaveIsWorking.log",FileMode.OpenOrCreate);
fs.Write(Encoding.ASCII.GetBytes(bstrURLItem),0,bstrURLItem.Length);
fs.Close();
}
}
catch (Exception ex)
{
throw (ex);
}
}

public void OnDelete(IExStoreEventInfo pEventInfo, string
bstrURLItem, int lFlags)
{
try
{

}
catch(Exception ex)
{
throw (ex);
}
}
#endregion


public void Process_mail(IExStoreEventInfo pEventInfo, string
bstrURLItem)
{
try
{
FileStream fs = new
FileStream(@"c:\temp\MyEventSink.log",FileMode.OpenOrCreate);
fs.Write(Encoding.ASCII.GetBytes(bstrURLItem),0,bstrURLItem.Length);
fs.Close();
}
catch(System.Runtime.InteropServices.COMException ee)
{
throw(ee);
}
catch(Exception tom)
{
throw (tom);
}

try
{
CDO.Message iMessage=new CDO.MessageClass();
ADODB.Stream stm;
CDO.IBodyPart iPrt;
int i;
int aNum;
string eName;
string fName;
string sFrom;
string sDate;
string sBody;
string sSubject;

iMessage.DataSource.Open(bstrURLItem,null,ADODB.ConnectModeEnum.adModeRead,ADODB.RecordCreateOptionsEnum.adFailIfNotExists,ADODB.RecordOpenOptionsEnum.adOpenSource,"","");

//IExStoreDispEventInfo
newEventInfo=(IExStoreDispEventInfo)pEventInfo;
//ADODB.Record record=(ADODB.Record)newEventInfo.EventRecord;


sSubject = iMessage.Subject;
sBody = iMessage.TextBody;
sFrom = iMessage.From;
sDate = iMessage.ReceivedTime.ToString();
aNum = iMessage.Attachments.Count;

FileStream fsb = new
FileStream(@"c:\temp\NEWEST_SUBJECT_BODY_MyEventSink.log",FileMode.OpenOrCreate);
fsb.Write(Encoding.ASCII.GetBytes(sSubject + "::" +
sBody),0,sSubject.Length + "::".Length + sBody.Length);
fsb.Close();


for(i=1;i<=aNum;i++)
{
fName = iMessage.Attachments.FileName;
eName = fName.Substring(fName.Length-3,3).ToUpper();
if(eName == "TXT")
{
iPrt = iMessage.Attachments;
iPrt.ContentMediaType = "text/plain";
iPrt.Charset = "windows-1250";
stm = iPrt.GetDecodedContentStream();
iPrt =null;
}
}
}
catch (Exception ex)
{
FileStream fs = new
FileStream(@"c:\temp\MyEventSinkERRORERROR.log",FileMode.OpenOrCreate);
fs.Write(Encoding.ASCII.GetBytes(ex.Message + ":::" +
ex.StackTrace),0,ex.Message.Length + ":::".Length +
ex.StackTrace.Length);
fs.Close();

throw (ex);
}
}


}
}
 
Top