2017-08-03 37 views
0

我正在尝试创建一个应用程序,它将使用ICS文件在目标用户的Outlook日历上创建,更新和取消约会。 我已经成功创建了ICS文件并首次成功发送给目标用户。 但我无法编辑并通过ICS文件取消约会。 这里是我当前的代码: -使用ICS文件更新和取消预约预约

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


    } 

而且在我的主要方法,我打了一个电话,如下所示: -

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(); 

现在我想更新,并通过ICS同样取消预约。 我试图实现通过以下链接的解决方案,但没有任何工程:

Cancel Outlook meeting

replace-existing-outlook-calendar-appointment

任何人都可以请指导我如何做到这一点? 在此先感谢。

回答

0

要取消预约使用:

"METHOD: CANCEL" 

要更新预约使用:

"METHOD: REQUEST" 

随后序列比创建约会的一个更大:

int sequence = appointment.Sequence + 1; 

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

Github的样品

对于样品的工作项目检查出我的实现在GitHub上:

https://github.com/pzarebski/AppointmentNotificationManager

+0

我试过,但更新的任命没有工作。 –

+0

附上您的约会ics文件和您的约会更新ics文件 – tinamou

+0

引用到您的Github项目https://github.com/pzarebski/AppointmentNotificationManager 现在就工作。谢谢 –