在Outlook Appointment的ICS文件中添加HTML

时间:2017-09-19 05:50:32

标签: c# asp.net outlook calendar icalendar

我正在尝试创建日历邀请,并希望在附加的ICS文件中添加HTML内容。

以下代码不起作用: -

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;
}

有人可以分享一些关于如何实现这一目标的意见吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

我终于设法解决了这个问题。 请在下面找到解决方案: -

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;
}

注意:我要在将其保存到“Sample.html”文件中之前缩小HTML。

希望它能帮助别人!!