在ICS文件上设置约会时间

时间:2017-04-11 15:35:25

标签: c# date time icalendar

我一直在尝试在我使用SMTP发送的ICS文件上设置约会日期和时间。该文件创建并发送得很好,并在到达时显示为会议/约会请求,但无论我尝试了什么,约会日期和时间始终是08:00的第二天。

这是我的代码,它已经从这里发布的各种旧例子拼凑而成。

这是代码

public void SendMeetingRequest(Models.Appointment app)
{
    //Create the meeting date
    var meetingDate = new DateTime(app.Date.Year,app.Date.Month,app.Date.Day,app.Time.Hour,app.Time.Minute,app.Time.Second);
    var endDate = meetingDate.AddHours(1);

    //Create new message
    MailMessage msg = new MailMessage();


    //Set message properties
    msg.From = new MailAddress("");
    msg.To.Add(new MailAddress(""));
    msg.Subject = app.Subject;//"Send mail with ICS file as an Attachment";
    msg.Body = "Please Attend the meeting with this schedule, " +  app.Body;
    msg.Headers.Add("Content-class", "urn:content-classes:calendarmessage");

    // Now Contruct the ICS file using string builder
    StringBuilder str = new StringBuilder();
    str.AppendLine("BEGIN:VCALENDAR");
    str.AppendLine("PRODID:-//Schedule a Meeting");
    str.AppendLine("VERSION:2.0");
    str.AppendLine("METHOD:REQUEST");
    str.AppendLine("BEGIN:VEVENT");
    str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", meetingDate)); 
    str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
    str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endDate));
    str.AppendLine("LOCATION");
    str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
    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(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("BEGIN:VALARM");
    str.AppendLine("TRIGGER:-PT15M");
    str.AppendLine("ACTION:DISPLAY");
    str.AppendLine("DESCRIPTION:Reminder");
    str.AppendLine("END:VALARM");
    str.AppendLine("END:VEVENT");
    str.AppendLine("END:VCALENDAR");
}

有人知道我做错了什么吗?

2 个答案:

答案 0 :(得分:0)

创建outlook calendar .ics文件。This is the reference link for outlook

希望这对你有用谢谢: - )

string schLocation = "Conference Room";
string schSubject = "Business visit discussion";
string schDescription = "Schedule description";
System.DateTime schBeginDate = Convert.ToDateTime("7/3/2008 10:00:00 PM");
System.DateTime schEndDate = Convert.ToDateTime("7/3/2008 11:00:00 PM");

//PUTTING THE MEETING DETAILS INTO AN ARRAY OF STRING

String[] contents = { "BEGIN:VCALENDAR",
                      "PRODID:-//Flo Inc.//FloSoft//EN",
                      "BEGIN:VEVENT",
                      "DTSTART:" + schBeginDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"), 
                      "DTEND:" + schEndDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"), 
                      "LOCATION:" + schLocation, 
                      "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + schDescription,
                      "SUMMARY:" + schSubject, "PRIORITY:3", 
                      "END:VEVENT", "END:VCALENDAR" };

以及下面提到的Gmail。This is the reference link for Gmail

str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+10)));
str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+60)));

再添加10分钟的开始时间可能对您有用。

答案 1 :(得分:0)

通过在VEVENT之前添加时区来管理时区

sw.AppendLine("TZ:+00");
sw.AppendLine("BEGIN:VEVENT");

然后创建这样的方法

public string ConvertToVCalendarDateString(DateTime d)
    {
        d = d.ToUniversalTime();
        string yy = d.Year.ToString();
        string mm = d.Month.ToString("D2");
        string dd = d.Day.ToString("D2");
        string hh = d.Hour.ToString("D2");
        string mm2 = d.Minute.ToString("D2");
        string ss = d.Second.ToString("D2");
        string s = yy + mm + dd + "T" + hh + mm2 + ss + "Z"; // Pass date as vCalendar format YYYYMMDDTHHMMSSZ (includes middle T and final Z) '
        return s;
    }

然后像下面那样传递您的开始日期和结束日期

sw.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", ConvertToVCalendarDateString(start)));
sw.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", ConvertToVCalendarDateString(end)));