8

I'm a beginner in C# (and any networking code to be honest). I'm trying to send a calendar invite, that will be wired when you click a button on the company's website. This is a typical n-tier system, using asp.net/C# and SQL.

We used to simply generate an ics that the user would then have to know to open with Outlook, but I've since learned how to manually code a VCALENDAR so it shows up right away in Outlook nice and neat.

It's all been going fairly smoothly, but I would now like the body of the calendar invite to be able to accept HTML, to attach links in particular. I've experimented with AlternateViews, but it seems that the "X-ALT-DESC" attribute inside of VCALENDAR should do exactly what I want. However, try as I may Outlook ignores it and uses the description. There is clearly something I am missing.

(To clarify, everything works & compiles, except for the HTML alt description)

private Guid? CreateEmail()
{

        Guid eventGuid = Guid.NewGuid();

        MailMessage msg = new MailMessage();
        msg.IsBodyHtml = true;
        msg.From = new MailAddress("fromemail", "From Name");
        msg.To.Add(toEmail);
        msg.Subject = subject;

        StringBuilder s = new StringBuilder();
        s.AppendLine("BEGIN:VCALENDAR");
        s.AppendLine("VERSION:2.0");
        s.AppendLine("PRODID:-//My Product//Outlook MIMEDIR//EN");
        s.AppendLine("METHOD:" + method); //In this case, "REQUEST"
        s.AppendLine("STATUS:" + status.status);  //"CONFIRMED"
        s.AppendLine("BEGIN:VEVENT");
        s.AppendLine("UID:" + eventGuid.ToString()); 
        s.AppendLine("PRIORITY" + status.priority); //3
        s.AppendLine("X-MICROSOFT-CDO-BUSYSTATUS:" + ShowAs.ToString()); //"BUSY"
        s.AppendLine("SEQUENCE:" + UpdateNumber);//0
        s.AppendLine("DTSTAMP:" + DateTime.Now.ToUniversalTime().ToString());
        s.AppendLine("DTSTART:" + DateTimetoCalTime(startTime));  
        s.AppendLine("DTEND:" + DateTimetoCalTime(endTime));
        s.AppendLine("SUMMARY:" + subject);
        s.AppendLine("LOCATION: " + location);
        s.AppendLine("DESCRIPTION: " + "Plain simple description"

 string html_begin = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">" +
            "\n<html>" +
            "\n<head>" +
            "\n<title></title>" +
            "\n</head>" +
            "\n<body>" +
            "\n<!-- Converted from text/rtf format -->\n\n<P DIR=LTR><SPAN LANG=\"en-us\">" +
            "\n<Font face=\"Times New Roman\"";

        body = "I simply <b> want some bold </b> here 555";

        string html_end = "</font></span></body>\n</html>";
        string html_body = html_begin + body + html_end;

        msg.Body = html_body;
        s.AppendLine("X-ALT-DESC;FMTTYPE=text/html:" + html_body);

        msg.Body = html_body;
        s.AppendLine("X-ALT_DESC;FMTTYPE=text/html:" + html_body);

        s.AppendLine("STATUS:" + status.status); //"CONFIRMED"
        s.AppendLine("BEGIN:VALARM");
        s.AppendLine("TRIGGER:-PT1440M");
        s.AppendLine("ACTION:Accept");
        s.AppendLine("DESCRIPTION:Reminder");
        s.AppendLine("END:VALARM");

        s.AppendLine("END:VEVENT");

        s.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", msg.To[0].DisplayName, msg.To[0].Address));
        s.AppendLine("END:VCALENDAR");

        System.Net.Mime.ContentType type = new System.Net.Mime.ContentType("text/calendar");
        type.Parameters.Add("method", method);
        type.Parameters.Add("name", "meeting.ics");
        msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(s.ToString(), type));

SMTP.send(msg);
return EventGuid;

Produces this body in outlook:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 3.2//EN”>
<html>
<head>
<title></title>
</head>
<body>
<!-- Converted from text/rtf format -->

<P DIR=LTR><SPAN LANG=”en-us”>
<Font face=”Times New Roman”I simply <b> want some bold </b> here 555</font></span></body>
</html>

From testing: If I leave Msg.body out, it just used the "DESCRIPTION". If I make it equal the HTML, I get the above result.

Thank You!

5
  • Have you tried removing the \n characters, or using \\n instead? Commented Jul 7, 2015 at 14:53
  • Removed all \n and I got Same output without line breaks...: <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 3.2//E N”><html><head><title></title></head><body><!-- Converted from text/rtf format --><P DIR=LTR><SPAN LANG=”en-us”><Font face=”Times New Roman”I simply <b> want some bold </b> here 11:07AM</font></span></body></html> Commented Jul 7, 2015 at 15:07
  • It could be a line-length issue. What if you try just the body? s.AppendLine("X-ALT-DESC;FMTTYPE=text/html:I simply <b>want some bold</b> here 555"); Commented Jul 7, 2015 at 15:20
  • Nope, tried that already. Commented Jul 7, 2015 at 15:31
  • Long time, I know, but ... typo? X-ALT_DESC vs X-ALT-DESC? I'm not a pro with the iCal specs (nor microsoft's extensions)
    – r2evans
    Commented Feb 19 at 21:39

6 Answers 6

5

You can have X-ALT-DESC on multiple lines, you just need to add a space on the beginning of each lines following it.

Lines of text SHOULD NOT be longer than 75 octets, excluding the line break. Long content lines SHOULD be split into a multiple line representations using a line "folding" technique. That is, a long line can be split between any two characters by inserting a CRLF immediately followed by a single linear white-space character (i.e., SPACE or HTAB). Any sequence of CRLF followed immediately by a single linear white-space character is ignored (i.e., removed) when processing the content type.

https://icalendar.org/iCalendar-RFC-5545/3-1-content-lines.html

1
  • The space is absolutely necessary. Without it, the new line gets ignored. This should be the correct answer. Commented Feb 7, 2019 at 20:58
3

I found that the HTML string must be all on one line. If the HTML is broken over multiple lines, that does not conform to Vcalendar encoding and the description is either rendered as a blank page or as plain text with all HTML tags visible.

I've seen others out there claiming that the DESCRIPTION tag must be used in front of "X-ALT-DESC;FMTTYPE=text/html:". This is totally WRONG and FALSE. If "DESCRIPTION" exists, it takes precedence, the "X-ALT-DESC;FMTTYPE=text/html:" line is completely ignored by Outlook and the plain text description is rendered. Therefore, "X-ALT-DESC;FMTTYPE=text/html:" must stand on it's own and be on it's own line.

Working example:

   ...
   X-ALT-DESC;FMTTYPE=text/html:<html><body><a href="http://bing.com">Bing</a></body></html>
   ...

Wrong:

   ...
   DESCRIPTION;X-ALT-DESC;FMTTYPE=text/html:<html><body><a href="http://bing.com">Bing</a></body></html>
   ...

Wrong again:

   ...
   X-ALT-DESC;FMTTYPE=text/html:<html>
   <body>
   <a href="http://bing.com">Bing</a>
   </body>
   </html>
   ...
2
  • also DESCRIPTION tag must be present. otherwise X-ALT-DESC ignored. example: DESCRIPTION:\n Commented May 7, 2019 at 5:01
  • @RamilGilfanov Not true, at least in Outlook 2016, a .ics without a DESCRIPTION will still work and display the X-ALT-DESC as the body
    – Kenbo
    Commented Oct 22, 2021 at 17:17
2

For those in the future: The problem was the use of .AppendLine. Simply use .Append

2
  • Wait ... this doesn't make any sense. Did you replace AppendLine with Append in the entire code sample above? Did you change the strings to include a \n at the end? Commented May 15, 2019 at 13:06
  • 1
    Yes, replaced wherever append was used. Didn't change anything else in the above example. You can have \n in the HTML, but the whole vcal needs to be on one line, thus appendLine was the culprit. (According to spec linked by @cslecours you can have it on one line, leaving appendLine as is, but each new line should start with a space). Commented May 15, 2019 at 13:10
0

The ics file which i am loading is not created with proper spaces which is longer than 75 octets, if i am manually adding space and loading to Ical.net.Calendar it works fine. But i want to do the same through c# code like manipulating the calendar file before loading to avoid parsing errors.

0
0

For reference, here's an explanation from https://icalendar.org/

"The original iCalendar standard allowed only plain text as part of an event description. HTML markup, such as font attributes (bold, underline) and layout (div, table) was not allowed in the text description field. First seen in Microsoft Outlook, the X-ALT-DESC parameter provides a method to add HTML to an event description. "X-" fields are allowed for non-standard, experimental parameters. This field has become the method of choice when including HTML in a description. When using HTML, both fields must be included so that iCalendar readers that do not support the X-ALT-DESC field can still read the text version."

0

...and it looks like Outlook 2016 dropped support for this. Generating ics files with html description only is most of the time not an option as Thunderbird/Lightening in the past did not handle this leading to calendar invites with empty body.

https://answers.microsoft.com/en-us/msoffice/forum/msoffice_outlook/outlook-2016-ics-description-shows-no-html/08d06cba-bfe4-4757-a052-adab64ea75a2?page=1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.