简体   繁体   中英

Setting appointment time on ICS file

I have been trying to set the appointment date and time on an ICS file that I am sending using SMTP. The file is created and sent just fine and displays as a meeting/appointment request when it arrives, but no matter what I have tried the appointment date and time are always the next day at 08:00.

This is my code which has been cobbled together from various old examples posted on here.

Here is the code

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

Anybody have any clue to what I am doing wrong?

To create outlook calendar .ics file. This is the reference link for outlook

Hope this work for you Thanks :-)

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

and below mention is for 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)));

Add 10 minutes more to start time it may work for you.

Manage Time zones by adding the time zone before VEVENT

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

then create a method like this

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

then pass your start and end date like below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM