D
D
Dmitry Vasilyuk2014-10-08 15:08:32
Email
Dmitry Vasilyuk, 2014-10-08 15:08:32

How to work with Vcalendar in C#?

There is a code that sends an email and a Vcalendar for an event that requires a meeting room. However, the application comes in a regular letter, not a notification. How to make the event notification display adequately in Outlook?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            btnSend_Click();
        }


        //-----------------------------------------------------------------------------------------------------------------------------------//
        private static void btnSend_Click()
        {
            //Declaration a list of attendees
            MailAddressCollection macCollection = new MailAddressCollection();
            //Add attendde. In this example, I send invite to only one
            macCollection.Add(new MailAddress("[email protected]"));
            //Create mail message
            MailMessage mmMessage = CreateMeetingRequest(DateTime.Now.AddHours(1), DateTime.Now.AddHours(2), "Test", "Test", "Test", "VasilukDmitrij", "[email protected]", macCollection);
            //Create smtp client
            SmtpClient smtp = new SmtpClient("127.0.0.1", 25);
            //Configure your smtp client
           // smtp.EnableSsl = false;
            smtp.Credentials = new NetworkCredential("[email protected]", "test");
            //Send it
            smtp.Send(mmMessage);
        }

        public static MailMessage CreateMeetingRequest(DateTime dtStart, DateTime dtEnd, string strSubject, string strSummary,
        string strLocation, string strOrganizerName, string strOrganizerEmail,
        MailAddressCollection macAttendeeList)
        {
            //Create an instance of mail message
            MailMessage mmMessage = new MailMessage();
            //  Set up the different mime types contained in the message
            System.Net.Mime.ContentType typeText = new System.Net.Mime.ContentType("text/plain");
            System.Net.Mime.ContentType typeHTML = new System.Net.Mime.ContentType("text/html");
            System.Net.Mime.ContentType typeCalendar = new System.Net.Mime.ContentType("text/vcalendar");

            //  Add parameters to the calendar header
            typeCalendar.Parameters.Add("method", "REQUEST");
            typeCalendar.Parameters.Add("name", "meeting.ics");

            //  Create message body parts in text format
            string strBodyText = "Type:Single Meeting\r\nOrganizer: {0}\r\nStart Time:{1}\r\nEnd Time:{2}\r\nTime Zone:{3}\r\nLocation: {4}\r\n\r\n*~*~*~*~*~*~*~*~*~*\r\n\r\n{5}";
            strBodyText = string.Format(strBodyText, strOrganizerName, dtStart.ToLongDateString() + " " + dtStart.ToLongTimeString(),
            dtEnd.ToLongDateString() + " " + dtEnd.ToLongTimeString(), System.TimeZone.CurrentTimeZone.StandardName,
            strLocation, strSummary);
            AlternateView viewText = AlternateView.CreateAlternateViewFromString(strBodyText, typeText);
            mmMessage.AlternateViews.Add(viewText);

            //Create the Body in HTML format
            string strBodyHTML = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\r\n<HTML>\r\n<HEAD>\r\n<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=utf-8\">\r\n<META NAME=\"Generator\" CONTENT=\"MS Exchange Server version 6.5.7652.24\">\r\n<TITLE>{0}</TITLE>\r\n</HEAD>\r\n<BODY>\r\n<!-- Converted from text/plain format -->\r\n<P><FONT SIZE=2>Type:Single Meeting<BR>\r\nOrganizer:{1}<BR>\r\nStart Time:{2}<BR>\r\nEnd Time:{3}<BR>\r\nTime Zone:{4}<BR>\r\nLocation:{5}<BR>\r\n<BR>\r\n*~*~*~*~*~*~*~*~*~*<BR>\r\n<BR>\r\n{6}<BR>\r\n</FONT>\r\n</P>\r\n\r\n</BODY>\r\n</HTML>";
            strBodyHTML = string.Format(strBodyHTML, strSummary, strOrganizerName, dtStart.ToLongDateString() + " " + dtStart.ToLongTimeString(),
            dtEnd.ToLongDateString() + " " + dtEnd.ToLongTimeString(), System.TimeZone.CurrentTimeZone.StandardName,
            strLocation, strSummary);
            AlternateView viewHTML = AlternateView.CreateAlternateViewFromString(strBodyHTML, typeHTML);
            mmMessage.AlternateViews.Add(viewHTML);

            //Create the Body in VCALENDAR format
            string strCalDateFormat = "yyyyMMddTHHmmssZ";
            string strBodyCalendar = "BEGIN:VCALENDAR\r\nMETHOD:REQUEST\r\nPRODID:Microsoft CDO for Microsoft Exchange\r\nVERSION:2.0\r\nBEGIN:VTIMEZONE\r\nTZID:(GMT-06.00) Central Time (US &amp; Canada)\r\nX-MICROSOFT-CDO-TZID:11\r\nBEGIN:STANDARD\r\nDTSTART:16010101T020000\r\nTZOFFSETFROM:-0500\r\nTZOFFSETTO:-0600\r\nRRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=11;BYDAY=1SU\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nDTSTART:16010101T020000\r\nTZOFFSETFROM:-0600\r\nTZOFFSETTO:-0500\r\nRRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=3;BYDAY=2SU\r\nEND:DAYLIGHT\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nDTSTAMP:{8}\r\nDTSTART:{0}\r\nSUMMARY:{7}\r\nUID:{5}\r\nATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=\"{9}\":MAILTO:{9}\r\nACTION;RSVP=TRUE;CN=\"{4}\":MAILTO:{4}\r\nORGANIZER;CN=\"{3}\":mailto:{4}\r\nLOCATION:{2}\r\nDTEND:{1}\r\nDESCRIPTION:{7}\\N\r\nSEQUENCE:1\r\nPRIORITY:5\r\nCLASS:\r\nCREATED:{8}\r\nLAST-MODIFIED:{8}\r\nSTATUS:CONFIRMED\r\nTRANSP:OPAQUE\r\nX-MICROSOFT-CDO-BUSYSTATUS:BUSY\r\nX-MICROSOFT-CDO-INSTTYPE:0\r\nX-MICROSOFT-CDO-INTENDEDSTATUS:BUSY\r\nX-MICROSOFT-CDO-ALLDAYEVENT:FALSE\r\nX-MICROSOFT-CDO-IMPORTANCE:1\r\nX-MICROSOFT-CDO-OWNERAPPTID:-1\r\nX-MICROSOFT-CDO-ATTENDEE-CRITICAL-CHANGE:{8}\r\nX-MICROSOFT-CDO-OWNER-CRITICAL-CHANGE:{8}\r\nBEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:REMINDER\r\nTRIGGER;RELATED=START:-PT00H15M00S\r\nEND:VALARM\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
            strBodyCalendar = string.Format(strBodyCalendar, dtStart.ToUniversalTime().ToString(strCalDateFormat), dtEnd.ToUniversalTime().ToString(strCalDateFormat),
            strLocation, strOrganizerName, strOrganizerEmail, Guid.NewGuid().ToString("B"), strSummary, strSubject,
            DateTime.Now.ToUniversalTime().ToString(strCalDateFormat), macAttendeeList.ToString());
            AlternateView viewCalendar = AlternateView.CreateAlternateViewFromString(strBodyCalendar, typeCalendar);
            viewCalendar.TransferEncoding = TransferEncoding.SevenBit;
            //mmMessage.AlternateViews.Add(viewCalendar);

            //Adress the message
            mmMessage.From = new MailAddress(strOrganizerEmail);
            foreach (MailAddress attendee in macAttendeeList)
            {
                mmMessage.To.Add(attendee);
            }
            mmMessage.Subject = strSubject;

            return mmMessage;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question