Howto: CDOSYS/C# Sending an ICS file

Todo:
1) Open an appointment in Outlook.    Save to file as an .ICS file.
2) Create a C# project and add the code below.
3) Do the TODO sections.

'TODO: Add a refernce to CDOSYS

using System.IO;
private void btnSendUsingIcs_Click(object sender, System.EventArgs e)
{
 string sFrom = "me@mycompany.com";      //  TODO: Change
 string sTo = "me@mycompany.com";  //  TODO: Change
 string sSubject = "This is my cal test";  //  TODO: Change
 string sBody = "This is the test body";  //  TODO: Change
 string sFile = "C:\\Inetpub\\wwwroot\\CSharpSendVcalEmail\\Myrecurringappt.ics";  //  TODO:

Change

 SendMessageWithICalFile(sFrom, sTo, sSubject, sBody, sFile);
}

public void SendMessageWithICalFile(string sFrom, string sTo, string sSubject, string sBody, string

sIcalPath)
{    
 //   'TO DO: Change these constants to match your environment
 
 string strSMTPSERVER = "SMTPhost";  // TODO: Change
  
 CDO.Message iMsg;
 CDO.IBodyPart iBpMsg;
 CDO.IBodyPart iBp1;
 ADODB.Fields Flds;
 ADODB.Stream Stm;

 iMsg = new CDO.Message();

 //Get IBodyPart on the Message object
 iBpMsg = iMsg.BodyPart;

 Flds = iBpMsg.Fields;
 Flds["urn:schemas:mailheader:Content-Type"].Value = "multipart/mixed";
 Flds.Update();

 //TEXT BODYPART
 // Add the body part for the text/plain part of message
 iBp1 = iBpMsg.AddBodyPart(-1);
 
 // set the fields here
 Flds = iBp1.Fields;
 Flds["urn:schemas:mailheader:content-type"].Value = "text/plain; charset=\"iso-8859-1\"";
 //Flds["urn:schemas:mailheader:Content-Transfer-Encoding"].Value = "quoted-printable";
 Flds.Update();
 
 //get the stream and add the message
 Stm = iBp1.GetDecodedContentStream();
 Stm.WriteText ("this is the message in text format", ADODB.StreamWriteEnum.stWriteLine);
 Stm.Flush();
  
 iBp1 = iBpMsg.AddBodyPart(-1);
 
 // set the content-type field here
 Flds = iBp1.Fields;
 Flds["urn:schemas:mailheader:Content-Type"].Value = "text/calendar";
 Flds.Update();

 Stm = iBp1.GetDecodedContentStream();
 Stm.LoadFromFile(sIcalPath);
 Stm.Flush();

 Flds = iBp1.Fields;
 //Flds["urn:schemas:mailheader:Content-Type"].Value = "text/calendar";
 Flds["urn:schemas:mailheader:Content-Type"].Value = "application/octet-stream;

filename=\"test.ics\"";  //TODO: Change test.ics to name of file
 Flds["urn:schemas:mailheader:Content-Transfer-Encoding"].Value = "base64";
 Flds["urn:schemas:mailheader:Content-Disposition"].Value = "attachment;

filename=\"test.ics\"";          //TODO: Change test.ics to name of file
 Flds.Update();
 
 CDO.Configuration iConf;
 iConf = new CDO.Configuration();
 Flds = iConf.Fields;

 //   Set the CDOSYS configuration fields to use port 25 on the SMTP server.
 Flds["https://schemas.microsoft.com/cdo/configuration/sendusing"].Value =

CDO.CdoSendUsing.cdoSendUsingPort;
 Flds["https://schemas.microsoft.com/cdo/configuration/smtpserver"].Value  = strSMTPSERVER;
 Flds["https://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"].Value  = 10;
 //Flds["https://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value  = 2; //

NTLM
 Flds.Update();
 iMsg.Configuration = iConf;

 iMsg.To = sTo;
 iMsg.From = sFrom;
 iMsg.Subject = sSubject;
 iMsg.TextBody = sBody;
 iMsg.Send();
}

Note: Programaticly creating a VCALENDAR is not supported outside of an Microsoft  API which is designed to create a VCALENDAR.  Please refer to the RFCs on how to create VCALENDAR content.

 Note: Creating a meeting request with CDOSYS/System.Web.Mail/System.Net.Mail is not supported.