ASHISH_TECHIT ASHISH_TECHIT - 1 month ago 14
ASP.NET (C#) Question

Add HTML in ICS file for Outlook Appointment

I'm trying to create a calendar invite and wants to add HTML content inside the attached ICS file.

The following code doesn't work:-

private static byte[] CreateiCal(int current_sequence, string guid, string subject, string location, DateTime startTime, DateTime endTime)
{
var a = new StringBuilder();
var sb = new StringBuilder();
a.Append("BEGIN:VCALENDAR\r\f");
a.Append("VERSION:2.0\r\f");
a.Append("PRODID:-//ince/events//NONSGML v1.0//EN\r\f");
a.Append("TZ:+00\r\f");
a.Append("BEGIN:VEVENT\r\f");
a.Append(String.Format("SEQUENCE:{0}\r\f", sequence.ToString()));
a.Append(String.Format("DTSTART:{0}\r\f", GetFormatedDate(startTime)));
a.Append(String.Format("DTEND:{0}\r\f", GetFormatedDate(endTime)));
a.Append(String.Format("LOCATION:{0}\r\f", location));
a.Append(String.Format("UID:{0}\r\f", guid));
a.Append(String.Format("SUMMARY:{0}\r\f", subject));

sb.Append("Sample Text1 \n");
sb.Append("Sample Text2 \n");
sb.Append(string.Format("Sample Text3 <a href='{0}'>LINK</a> \n", "www.google.com"));
sb.Append("Sample Text4 \n");

a.Append(String.Format("DESCRIPTION;X-ALT-DESC;FMTTYPE=text/html:"+sb.ToString() + "\r\f"));

a.Append((string.Format("ORGANIZER:MAILTO:{0}\r\f", "mailID@corporate.com")));

a.Append((string.Format("ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=FALSE;X-NUM-GUESTS=0:mailto:{0}\r\f", "mailID2@corporate.com")));


a.Append(String.Format("X-MICROSOFT-CDO-APPT-SEQUENCE:{0}\r\f", current_sequence.ToString()));

a.Append("BEGIN:VALARM\r\f");
a.Append("TRIGGER:-PT15M\r\f");
a.Append("REPEAT:2\r\f");
a.Append("DURATION:PT15M\r\f");
a.Append("ACTION:DISPLAY\r\f");
a.Append("DESCRIPTION:Reminder\r\f");
a.Append("END:VALARM\r\f");
a.Append("END:VEVENT\r\f");
a.Append("END:VCALENDAR\r\f");
byte[] b = Encoding.ASCII.GetBytes(a.ToString());
return b;
}


Can anyone please share some inputs on how to achieve this?

Thanks in advance.

Answer Source

I finally managed to resolved this. Please find below the solution:-

private static byte[] CreateiCal(int current_sequence, string guid, string subject, string location, DateTime startTime, DateTime endTime)
{

            var a = new StringBuilder();
            int sequence = current_sequence + 1;

            a.Append("BEGIN:VCALENDAR\r\f");
            a.Append("VERSION:2.0\r\f");
            a.Append("PRODID:-//ince/events//NONSGML v1.0//EN\r\f");

            a.Append("TZ:+00\r\f");
            a.Append("BEGIN:VEVENT\r\f");
            a.Append(String.Format("SEQUENCE:{0}\r\f", sequence.ToString()));
            a.Append(String.Format("DTSTART:{0}\r\f", GetFormatedDate(startTime)));
            a.Append(String.Format("DTEND:{0}\r\f", GetFormatedDate(endTime)));
            a.Append(String.Format("LOCATION:{0}\r\f", location));
            a.Append(String.Format("UID:{0}\r\f", guid));

            a.Append(String.Format("SUMMARY:{0}\r\f", subject));

            string desc = File.ReadAllText("Sample.html", Encoding.GetEncoding("iso-8859-1")); // iso-8859-1 : HTML contains French characters

            a.Append("X-ALT-DESC;FMTTYPE=text/html:" + desc + "\r\f");

            a.Append(String.Format("DESCRIPTION:{0}\r\f", desc));


            a.Append(String.Format("X-MICROSOFT-CDO-APPT-SEQUENCE:{0}\r\f", current_sequence.ToString()));

            a.Append("BEGIN:VALARM\r\f");
            a.Append("TRIGGER:-PT15M\r\f");
            a.Append("REPEAT:2\r\f");
            a.Append("DURATION:PT15M\r\f");
            a.Append("ACTION:DISPLAY\r\f");
            a.Append("DESCRIPTION:Reminder\r\f");

            a.Append("END:VALARM\r\f");
            a.Append("END:VEVENT\r\f");


            a.Append("END:VCALENDAR\r\f");

            byte[] b = Encoding.UTF8.GetBytes(a.ToString());

            return b;
}

NOTE:- I've to minify the HTML before Saving it in the "Sample.html" file.

Hope it helps others!!