HOWTO CDOEX C#: How to extract VCalendar stream from an appointment.

' TODO:
'    Create a C# Winform app
'    Reference to Microsoft CDO for Exchange 2000 Server Library (COM) - this
'      will add a reference to ADO automaticlly.
'    NOTE: Do not add a reference to ADO or ADO.NET - this will cause interferance
'      with CDOEX/CDOSYS (which you may not see until processing body parts).
'    NOTE: ADO.NET is not supported working against CDOEX or CDOSYS.
'    Add a button.
'   Replace the button click event code with the code below

' This code will loop through the appointment.bodypart.bodyparts collection and extract the stream
'  text of the body part having theContentMediaType set to "text/calendar".

private void button1_Click(object sender, System.EventArgs e)
{
 CDO.IFolder  iFldr = new CDO.FolderClass();
 CDO.IDataSource iDsrc;
 ADODB.Connection Conn = new ADODB.Connection();
 String Url1;
 CDO.IBodyPart iBp; // for IBodyPart on message
 CDO.IBodyPart iBp2; // for IBodyPart on message
 CDO.IBodyPart iBp3; // for IBodyPart on message

 ADODB.Stream oStream2;
 ADODB.Stream oStream3;
 String strText = "";
 String sContentMediaType;

 Url1 = "file://./backofficestorage/myserver.extest.microsoft.com/MBX/administrator/";

 Conn.Provider = "ExOLEDB.DataSource";
 Conn.Open(Url1.ToString(), "Administrator", "adminpassword", -1);

 CDO.Appointment iAppt ;
 iAppt = new CDO.Appointment();

 iDsrc = iAppt.DataSource;

 iDsrc.Open (Url1 + "/calendar/subject.eml", Conn,
  ADODB.ConnectModeEnum.adModeReadWrite, 
  ADODB.RecordCreateOptionsEnum.adFailIfNotExists, 
  ADODB.RecordOpenOptionsEnum.adOpenSource, 
  "Administrator", "adminpassword");
   
 iBp = iAppt.BodyPart;

 IEnumerator iApptBPEnum = iAppt.BodyPart.BodyParts.GetEnumerator();
 while (iApptBPEnum.MoveNext() )
 {
  iBp2 = (CDO.IBodyPart)iApptBPEnum.Current;

  //Process body part code here
  sContentMediaType = iBp2.ContentMediaType.ToString();
  if (sContentMediaType == "text/calendar")
  {
   oStream2 = iBp2.GetStream();
   iBp2.SaveToFile ("c:\\apptcal.ical");
  }
  else
  {
   strText = iBp2.ToString();
   IEnumerator iApptBPEnum2 = iBp2.BodyParts.GetEnumerator();
   while (iApptBPEnum2.MoveNext() )
   {
    iBp3 = (CDO.IBodyPart)iApptBPEnum2.Current;

    //Process body part code here
    sContentMediaType = iBp3.ContentMediaType.ToString();
    if (sContentMediaType == "text/calendar")
    {
     oStream3 = iBp3.GetStream();
     iBp3.SaveToFile ("c:\\apptcal.ical");
    }
   }
  }
 }
 iBp = null; // Release
}