【问题标题】:Setting appointment time on ICS file在 ICS 文件上设置预约时间
【发布时间】:2017-04-11 15:35:24
【问题描述】:

我一直在尝试在我使用 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");
}

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

【问题讨论】:

  • 对不起图片,我不经常使用这个网站,我认为它实际上会将图片放入帖子中。
  • 抱歉,我从传入的模型中得到 app.Date.Year,它是 DateTime 数据类型。
  • 不幸的是,我已将其更改为您的建议,但现在我收到 System.Format 异常,说明字符串不是有效的 DateTime。
  • 您应该将实际代码放在您的问题中,而不是代码的屏幕截图。我们不使用 Photoshop 编程。
  • 对不起,我现在已经在帖子中包含了代码。

标签: c# date time icalendar


【解决方案1】:

创建 Outlook 日历 .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 分钟,这可能对您有用。

【讨论】:

  • 我已经尝试了以上两种方法,但不幸的是没有成功。
【解决方案2】:

通过在 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)));

【讨论】:

    猜你喜欢
    • 2018-01-10
    • 2013-06-08
    • 1970-01-01
    • 2020-05-28
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    相关资源
    最近更新 更多