Send a Calendar Invitation from c# using vCalendar

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.IO;

namespace CalendaringScheduling
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sbCalendar = new StringBuilder();
            DateTime dtStart = DateTime.Now;
            DateTime dtEnd = DateTime.Now.AddHours(2);

            sbCalendar.AppendLine("METHOD: REQUEST");
            sbCalendar.AppendLine("BEGIN:VCALENDAR");
            sbCalendar.AppendLine("PRODID:-//Ian Chivers//NET");
            sbCalendar.AppendLine("MIMEDIR//ENVERSION:1.0");
            sbCalendar.AppendLine("METHOD:REQUEST");
            sbCalendar.AppendLine("BEGIN:VEVENT");
            sbCalendar.AppendLine("DTSTAMP:" + dtStart.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
            sbCalendar.AppendLine("DTSTART:" + dtStart.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
            sbCalendar.AppendLine("DTEND:" + dtEnd.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
            sbCalendar.AppendLine("LOCATION:Minstead");
            sbCalendar.AppendLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:My first meeting");
            sbCalendar.AppendLine("SUMMARY:Learning Calendaring and Scheduling");
            sbCalendar.AppendLine("PRIORITY:3");
            sbCalendar.AppendLine("UID:" + Guid.NewGuid().ToString() + "@ianchivers.com");
            sbCalendar.AppendLine("ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION:MAILTO:ian.chivers@triangle-group.com");
            sbCalendar.AppendLine("ATTENDEE;ROLE=CHAIR;PARTSTAT=ACCEPTED:MAILTO:ian@ianchivers.com");
            sbCalendar.AppendLine("CLASS:PUBLIC");
            sbCalendar.AppendLine("ORGANIZER:MAILTO:ian@ianchivers.com");
            sbCalendar.AppendLine("SEQUENCE:0");
            sbCalendar.AppendLine("STATUS:TENTATIVE");
            sbCalendar.AppendLine("END:VEVENT");
            sbCalendar.AppendLine("END:VCALENDAR");

            byte[] byteArray = Encoding.UTF8.GetBytes(sbCalendar.ToString());

            Stream contentStream = new MemoryStream(byteArray);

            SmtpClient smtp = new SmtpClient("mx1.hotmail.co.uk");
            MailMessage memo = new MailMessage("ian@ianchivers.com", "ian_chivers@hotmail.co.uk");
            Attachment attachment = new Attachment(contentStream, "calendar.ics", "text/calendar");
            attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
            memo.Attachments.Add(attachment);
            smtp.Send(memo);

        }
    }
}

1 comments:

Happy Cooking said...

Hi Ian,

Your code really helped me in sending calendar invitation. I have got strucked up in sending a recurring mail invitation. Could you please help me in sending a Recurring meeting invitation. Your help will be appreciable. Thanks in advance.

-Raghu