我尝试创建一个应用程序,该应用程序将使用ICS文件在目标用户的Outlook日历上创建,更新和取消约会。 我已成功创建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("fistname.lastname@server.com", "person2.surname@server.com", "Title", "Subject", startTime, endTime, "Location");
Console.ReadLine();
现在我想通过ICS更新和取消相同的约会。 我尝试使用以下链接实现解决方案,但没有任何作用:
replace-existing-outlook-calendar-appointment
任何人都可以指导我如何做到这一点? 提前谢谢。
答案 0 :(得分:1)
取消预约使用:
"METHOD: CANCEL"
更新预约使用:
"METHOD: REQUEST"
后面的序列大于创建约会的序列:
int sequence = appointment.Sequence + 1;
"SEQUENCE:" + sequence .ToString()
"X-MICROSOFT-CDO-APPT-SEQUENCE:" + currentSequence .ToString()
Github示例
对于示例工作项目,请查看我在GitHub上的实现: