It's not too well documented, but Googling around I see that I'm not the only one needing to pop out this stuff from a custom app.
To cut to the chase
A few words about the code
Compatibility
The above code has been tested to work with both ThunderBird 2.0 and Outlook 2003.
Ref
http://weblogs.asp.net/bradvincent/archi ve/2008/01/16/creating-vcalendars-progra mmatically.aspx
To cut to the chase
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
ContentType CalendarType = new ContentType("text/calendar");
CalendarType.Parameters.Add("method", "REQUEST");
CalendarType.Parameters.Add("name", "meeting.ics");
string body =
"BEGIN:VCALENDAR\n" +
"PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN\n" +
"VERSION:2.0\n" +
"METHOD:REQUEST\n" +
"BEGIN:VEVENT\n" +
"ATTENDEE;RSVP=TRUE:MAILTO:unused@mail.c om\n" +
"DTSTART:20090304T000000Z\n" +
"DTEND:20090304T013520Z\n" +
"UID:4639843788467846786478364\n" +
"DTSTAMP:20090304T013520Z\n" +
"DESCRIPTION:appointment description here" +
"BEGIN:VALARM\n" +
"TRIGGER:-PT15M\n" +
"ACTION:DISPLAY\n" +
"DESCRIPTION:enter\n description here\n" +
"END:VALARM\n" +
"END:VEVENT\n" +
"END:VCALENDAR\n";
AlternateView view = AlternateView.CreateAlternateViewFromStr ing(body, CalendarType);
view.TransferEncoding = TransferEncoding.QuotedPrintable;
MailMessage mail = new MailMessage("sender@mail.com", "recipient@mail.com", "mail header", "body");
mail.AlternateViews.Add(view);
SmtpClient smtp = new SmtpClient("smtp.mail.com");
smtp.Credentials = new NetworkCredential("username", "password");
smtp.Send(mail);A few words about the code
- MailMessage does not support directly adjusting the ContentType header. So the only way out is by appending a view to the message, and setting the ContentType for that view.
- One has to be careful with taking out seemingly unused information in the VCalendar string. Til now I'm not sure why I have to include the MAILTO attribute, seeing that both Thunderbird and Outlook do not use it. Things like PROGID is also sensitive to Outlook.
- This method does not work in older Outlook (iirc 2000 and before), which uses a proprietary appointment format.
- If your DESCRIPTION is going to be more than a line long, remember to add a space at the start of each line, so Outlook knows it's still part of DESCRIPTION. A new line in the body != a new line in the description. Break up the lines yourself, because Outlook does not like it the way MailMessage does it.
- UID needs to be different every time you generate a new appointment. Outlook uses this as the appointment ID. You take responsibility for making sure no 2 appointments generate the same id. It's not that hard.
- The code is mostly complete. But don't expect to copy-n-paste and expect it to work. Some basic C# experience is required.
Compatibility
The above code has been tested to work with both ThunderBird 2.0 and Outlook 2003.
Ref
http://weblogs.asp.net/bradvincent/archi

