5

I can programmatically create a meeting request that is sent to the user through code and appears in Outlook mail where the user can accept the request and if accepted the appointment shows on their Outlook calendar. but am having trouble figuring out how to programmatically cancel the same event.

The below code is what I am using to send the meeting invitation. It works as should and sends the request to the recipient and they can accept or decline. If accepted the appointment goes on their calendar.

 Dim smtpServer As String = ConfigurationManager.AppSettings("MailServer").ToString()
 Dim credentials As New NetworkCredential(ConfigurationManager.AppSettings("SMTPUser").ToString(), ConfigurationManager.AppSettings("SMTPPassword").ToString())

 Dim startTime1 As String = Convert.ToDateTime("10/30/2015 11:00 AM").ToString("yyyyMMddTHHmmss")
 Dim endTime1 As String = Convert.ToDateTime("10/30/2015 01:00 PM").ToString("yyyyMMddTHHmmss")
 Dim smtp As New SmtpClient(smtpServer)
 smtp.Credentials = credentials

 Dim msg As New MailMessage()
 Dim emailFrom As String = ConfigurationManager.AppSettings("EmailFrom").ToString()
 Dim emailTo As String = "jd@dom.com"
 msg.From = New MailAddress(emailFrom, "Scheduling System")
 msg.[To].Add(New MailAddress(emailTo))
 msg.Subject = "JD"

 Dim strBody As New StringBuilder()
 strBody.AppendLine("Appointment Confirmation")
 strBody.AppendLine("Subject: JD")
 strBody.AppendLine("1599")
 strBody.AppendLine("Location: Exam 1")
 strBody.AppendLine("Date: 10/30/2015")
 strBody.AppendLine("Time: 11:00AM - 1:00PM")

 msg.Body = strBody.ToString()

 Dim str As New StringBuilder()
 str.AppendLine("BEGIN:VCALENDAR")

 'PRODID: identifier for the product that created the Calendar object
 str.AppendLine("PRODID:-//CARS//Outlook MIMEDIR//EN")
 str.AppendLine("VERSION:2.0")
 str.AppendLine("METHOD:REQUEST")

 str.AppendLine("BEGIN:VEVENT")

 str.AppendLine(String.Format("DTSTART:{0:yyyyMMddTHHmmss}", startTime1))
 'TimeZoneInfo.ConvertTimeToUtc("BeginTime").ToString("yyyyMMddTHHmmssZ")));
 str.AppendLine(String.Format("DTSTAMP:{0:yyyyMMddTHHmmss}", DateTime.Now))
 str.AppendLine(String.Format("DTEND:{0:yyyyMMddTHHmmss}", endTime1))       
 'TimeZoneInfo.ConvertTimeToUtc("EndTime").ToString("yyyyMMddTHHmmssZ")));
 str.AppendLine(String.Format("LOCATION:{0}", "Exam 1"))

 ' UID should be unique.
 str.AppendLine(String.Format("UID:{0}", "jd101"))
 str.AppendLine(String.Format("DESCRIPTION:{0}", msg.Body))
 str.AppendLine(String.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body))
 str.AppendLine(String.Format("SUMMARY:{0}", msg.Subject))

 str.AppendLine("STATUS:CONFIRMED")
 str.AppendLine("BEGIN:VALARM")
 str.AppendLine("TRIGGER:-PT15M")
 str.AppendLine("ACTION:Accept")
 str.AppendLine("DESCRIPTION:Reminder")
 str.AppendLine("X-MICROSOFT-CDO-BUSYSTATUS:BUSY")
 str.AppendLine("END:VALARM")
 str.AppendLine("END:VEVENT")

 str.AppendLine(String.Format("ORGANIZER:MAILTO:{0}", msg.From.Address))
 str.AppendLine(String.Format("ATTENDEE;CN=""{0}"";RSVP=TRUE:mailto:{1}", msg.[To](0).DisplayName, msg.[To](0).Address))

 str.AppendLine("END:VCALENDAR")
 Dim ct As New System.Net.Mime.ContentType("text/calendar")
 ct.Parameters.Add("method", "REQUEST")
 ct.Parameters.Add("name", "meeting.ics")
 Dim avCal As AlternateView = AlternateView.CreateAlternateViewFromString(str.ToString(), ct)
 msg.AlternateViews.Add(avCal)
 smtp.Send(msg)

The below code is what I have to CANCEL an existing meeting. It sends the notice out just like the above code does, but it does not cancel/delete/remove the meeting. Can someone point me in the right direction please. I would just like the event to be removed from the Outlook calendar when this part of the code is ran. Thanks for any help.

Dim smtpServer As String = ConfigurationManager.AppSettings("MailServer").ToString()
Dim credentials As New NetworkCredential(ConfigurationManager.AppSettings("SMTPUser").ToString(), ConfigurationManager.AppSettings("SMTPPassword").ToString())

Dim startTime1 As String = Convert.ToDateTime("10/30/2015 11:00 AM").ToString("yyyyMMddTHHmmss")
Dim endTime1 As String = Convert.ToDateTime("10/30/2015 01:00 PM").ToString("yyyyMMddTHHmmss")
Dim smtp As New SmtpClient(smtpServer)
smtp.Credentials = credentials

Dim msg As New MailMessage()
Dim emailFrom As String = ConfigurationManager.AppSettings("EmailFrom").ToString()
Dim emailTo As String = "jd@dom.com"
msg.From = New MailAddress(emailFrom, "Scheduling System")
msg.[To].Add(New MailAddress(emailTo))
msg.Subject = "JD"

Dim strBody As New StringBuilder()
strBody.AppendLine("Appointment Confirmation")
strBody.AppendLine("Subject: JD")
strBody.AppendLine("HRPO#: 1599")
strBody.AppendLine("Location: Exam 1")
strBody.AppendLine("Date: 10/30/2015")
strBody.AppendLine("Time: 11:00AM - 1:00PM")

msg.Body = strBody.ToString()

Dim str As New StringBuilder()
str.AppendLine("BEGIN:VCALENDAR")

'PRODID: identifier for the product that created the Calendar object
str.AppendLine("PRODID:-//CARS//Outlook MIMEDIR//EN")
str.AppendLine("VERSION:2.0")
str.AppendLine("METHOD:REQUEST")

str.AppendLine("BEGIN:VEVENT")

str.AppendLine(String.Format("DTSTART:{0:yyyyMMddTHHmmss}", startTime1))
'TimeZoneInfo.ConvertTimeToUtc("BeginTime").ToString("yyyyMMddTHHmmssZ")));
str.AppendLine(String.Format("DTSTAMP:{0:yyyyMMddTHHmmss}", DateTime.Now))
str.AppendLine(String.Format("DTEND:{0:yyyyMMddTHHmmss}", endTime1))
'TimeZoneInfo.ConvertTimeToUtc("EndTime").ToString("yyyyMMddTHHmmssZ")));
str.AppendLine(String.Format("LOCATION:{0}", "Exam 1"))

' UID should be unique.
str.AppendLine(String.Format("UID:{0}", "jd101"))
str.AppendLine(String.Format("DESCRIPTION:{0}", msg.Body))
str.AppendLine(String.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body))
str.AppendLine(String.Format("SUMMARY:{0}", msg.Subject))

str.AppendLine("STATUS:CANCELLED")
str.AppendLine("BEGIN:VALARM")
str.AppendLine("TRIGGER:-PT15M")
str.AppendLine("ACTION:Accept")
str.AppendLine("DESCRIPTION:Reminder")
str.AppendLine("X-MICROSOFT-CDO-BUSYSTATUS:BUSY")
str.AppendLine("END:VALARM")
str.AppendLine("END:VEVENT")

str.AppendLine(String.Format("ORGANIZER:MAILTO:{0}", msg.From.Address))
str.AppendLine(String.Format("ATTENDEE;CN=""{0}"";RSVP=TRUE:mailto:{1}", msg.[To](0).DisplayName, msg.[To](0).Address))

str.AppendLine("END:VCALENDAR")
Dim ct As New System.Net.Mime.ContentType("text/calendar")
ct.Parameters.Add("method", "CANCEL")
ct.Parameters.Add("name", "meeting.ics")
Dim avCal As AlternateView = AlternateView.CreateAlternateViewFromString(str.ToString(), ct)
msg.AlternateViews.Add(avCal)
smtp.Send(msg)
Jeremy Dodt
  • 125
  • 1
  • 8
  • 1
    I couldn't get the X-MICROSOFT-CDO-BUSYSTATUS:BUSY status to work between the BEGIN:VALARM / END:VALARM code. I put it before str.AppendLine("BEGIN:VALARM") and then it displayed the BUSY status as the default. I'm using it with Outlook 2013. – thecoolmacdude Dec 02 '15 at 14:30
  • Great info thecoolmacdude! We're using 2010 and it works as should, but this is great to know for future changes/upgrades. During my research I found that each version of office treated things differently and there were even many cases that stated for security reasons this wouldn't work with the newer versions. Thanks again for the input. – Jeremy Dodt Dec 03 '15 at 14:47

2 Answers2

4

ANSWERED

To cancel the meeting and have it removed from the outlook calendar you need to change the Method from "REQUEST" to "CANCEL" for the event that sends the cancellation request.

msg.Body = strBody.ToString()

Dim str As New StringBuilder()
str.AppendLine("BEGIN:VCALENDAR")

'PRODID: identifier for the product that created the Calendar object
str.AppendLine("PRODID:-//CARS//Outlook MIMEDIR//EN")
str.AppendLine("VERSION:2.0")
'''ORIGINAL-CHANGE TO CANCEL'''
'str.AppendLine("METHOD:REQUEST")
'''NEW - CHANGE TO CANCEL'''
str.AppendLine("METHOD:CANCEL")
'''Everything else remains the same. Will work and remove meeting from calendar.'''
Jeremy Dodt
  • 125
  • 1
  • 8
0

Currently i am using this code to send meeting to outlook..

StringBuilder OutlookBody = new StringBuilder();

string textvs = @"BEGIN:VCALENDAR

PRODID:-//Microsoft Corporation//Outlook 10.0 MIMEDIR//EN

VERSION:1.0

BEGIN:VEVENT

LOCATION:" + Location + @"

DTSTART:" + string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", start) + @"

DTEND:" + string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", end) + @"

DESCRIPTION;ENCODING=QUOTED-PRINTABLE:= " + OutlookBody + @"=0D=0A SUMMARY:" + AppoitmentName + @"

PRIORITY:3

END:VEVENT

END:VCALENDAR";

And it is working fine..

How can i use the same code to cancel / remove appointment from outlook.

Deepak gupta
  • 1,938
  • 11
  • 11
  • Add METHOD:CANCEL to cancel the appointment. You will also need a UID to reference. In the original example it shows the UID when sent, use the same UID when cancelling. I use the autoId(PK) in the database as my UID. As a security feature you can not directly cancel the appointment(just as you can not directly place it on the users calendar), but request a cancellation and the user can remove from calendar. Hope this helps. – Jeremy Dodt Feb 01 '17 at 18:53