ASHISH_TECHIT
ASHISH_TECHIT

Reputation: 127

Update and cancel outlook appointment using ICS file

I'm trying to create an application which will create, update and cancel an appointment on target user's outlook calendar using ICS file. I've successfully created the ICS file and send to target user's successfully for the first time. But I'm unable to edit and cancel the appointment through an ICS file. Here's my current code:-

private static byte[] CreateiCal(string subject, string location, DateTime startTime, DateTime endTime)
    {
        var a = new StringBuilder();
        var guid = Guid.NewGuid();

        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("BEGIN:VEVENT\r\f");
        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));
        a.Append(String.Format("DESCRIPTION:{0}\r\f", subject));


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

    private static string GetFormatedDate(DateTime date)
    {
        return String.Format("{0}{1}{2}T{3}{4}00Z", date.ToUniversalTime().Year, date.ToUniversalTime().Month.ToString("00"), date.ToUniversalTime().Day.ToString("00"), date.ToUniversalTime().Hour.ToString("00"), date.ToUniversalTime().Minute.ToString("00"));
    }

    private String CreateBody(String title, String description, String location, DateTime startTime, DateTime endTime)
    {
        const String successStyle = ".success{color: #333333;font-family: Century Gothic, Arial;font-size:1.4em;margin-top:5px;margin-bottom:5px;}";
        const String standardStyle = ".standard{color: #333333;font-family: Century Gothic, Arial;font-size:1.0em}";
        const String reminderStyle = ".reminderStyle{color: red;font-family: Century Gothic, Arial;font-size:1.0em;font-variant:italic;margin-top:2px;margin-bottom:2px;}";
        const String contentsStyle = ".contentsTable{color: #333333;font-family: Century Gothic, Arial;font-size:1.0em;border-collapse:collapse;border-spacing:0px;padding:0px;margin:0px;} .contentsTable > td {padding-right:5px;}";
        var b = new StringBuilder();
        b.Append(String.Format("<style>{0} {1} {2} {3}</style>", successStyle, standardStyle, contentsStyle, reminderStyle));
        b.Append(String.Format("<div class=\"standard\">You have successfully registered for the following event</div>"));
        b.Append(String.Format("<div class=\"success\">{0}</div>", title));
        b.Append(String.Format("<div class=\"reminderStyle\">Please click on the calendar invitation to add this appointment to your calendar.</div>"));
        b.Append(String.Format("<table class=\"contentsTable\">"));
        b.Append(String.Format("<tr class=\"standard\"><td>Time</td><td>{0} - {1}</td></tr>", startTime, endTime));
        b.Append(String.Format("<tr class=\"standard\"><td>Location</td><td>{0}</td></tr>", location));
        b.Append(String.Format("<tr class=\"standard\"><td>Description</td><td>{0}</td></tr>", description));
        b.Append(String.Format("</table>"));
        return b.ToString();
    }

    public void SendAppointment(string from, string to, string title, string body, DateTime startTime, DateTime endTime, String location)
    {
        try
        {
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();


            smtpClient.Host = "my.smtp.server";

            smtpClient.Port = 25;

            smtpClient.EnableSsl = false;

            smtpClient.UseDefaultCredentials = true;


            message.From = new MailAddress(from);
            message.To.Add(new MailAddress(to));
            message.Subject = "Successfully registered";
            string result = "Sample text";

            message.Body = "<html><head><body>" + CreateBody(title, body, location, startTime, endTime) +  "</body></head></html>";
            message.IsBodyHtml = true;

            byte[] attachment = CreateiCal(title, location, startTime, endTime);
            var ms = new MemoryStream(attachment);
            message.Attachments.Add(new Attachment(ms, "eventappointment.ics", "text/calendar"));

            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

            smtpClient.Send(message);

            string sMessage = "Email sent.";

            Console.WriteLine(sMessage);

        }
        catch (Exception ex)
        {
            Console.WriteLine("Couldn't send the mail!" + Environment.NewLine + ex.Message);
        }


    }

And in my main method I made a call as given below:-

DateTime startTime = new DateTime(2017, 08, 29, 14, 15, 0);
DateTime endTime = new DateTime(2017, 08, 30, 16, 35, 0);
CreateICS objCreateICS = new CreateICS();
objCreateICS.SendAppointment("[email protected]", "[email protected]", "Title", "Subject", startTime, endTime, "Location");
Console.ReadLine();

Now I want to update and cancel the same appointment through ICS. I've tried to implement solution using below links, but nothing works :

Cancel Outlook meeting

replace-existing-outlook-calendar-appointment

Can anyone please guide me on how to do this? Thanks in advance.

Upvotes: 1

Views: 5541

Answers (1)

tinamou
tinamou

Reputation: 2291

To cancel appointment use:

"METHOD: CANCEL"

To update appointment use:

"METHOD: REQUEST"

followed by sequence greater than the one which created the appointment:

int sequence = appointment.Sequence + 1;

"SEQUENCE:" + sequence .ToString()
"X-MICROSOFT-CDO-APPT-SEQUENCE:" + currentSequence .ToString()

Github Sample

For sample working project check out my implementation on GitHub:

https://github.com/pzarebski/AppointmentNotificationManager

Upvotes: 1

Related Questions