Howto: CDOSYS/C# Sending an ICS file

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 = "";      //  TODO: Change
 string sTo = "";  //  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:


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

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

 //   '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";

 // 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";
 //get the stream and add the message
 Stm = iBp1.GetDecodedContentStream();
 Stm.WriteText ("this is the message in text format", ADODB.StreamWriteEnum.stWriteLine);
 iBp1 = iBpMsg.AddBodyPart(-1);
 // set the content-type field here
 Flds = iBp1.Fields;
 Flds["urn:schemas:mailheader:Content-Type"].Value = "text/calendar";

 Stm = iBp1.GetDecodedContentStream();

 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
 CDO.Configuration iConf;
 iConf = new CDO.Configuration();
 Flds = iConf.Fields;

 //   Set the CDOSYS configuration fields to use port 25 on the SMTP server.
 Flds[""].Value =

 Flds[""].Value  = strSMTPSERVER;
 Flds[""].Value  = 10;
 //Flds[""].Value  = 2; //

 iMsg.Configuration = iConf;

 iMsg.To = sTo;
 iMsg.From = sFrom;
 iMsg.Subject = sSubject;
 iMsg.TextBody = sBody;

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.

Comments (1)

Skip to main content