Sending a Appointment programmatically through Code , ASP.NET ,ICalendar Format
Here is in which you can send appointments via ICalendar format through code and not using the Outlook Object library (This is a very basic version of the way and does not involve much exception handling,and doesnt take care of nth case , explore a bit on that front :))
I am going to show the method of sending an appointment by creating a ICalendar Appointment file first and then sending it as an attachement over email. (The way in which ICalendar format files are sent when you click on Tools ->Export -> as ICalendar file in the appointment )
Here is the declaration for the TimeFormat and some variables used to fill the Appoinment details. viz start time,endtime etc.
Create a Skeleton for the appointment ICalendar file format and using the variables created above we will assign the values accordingly into the Appoinment skeleton. (This string concatation can be optimised by using string builder)
Assign the Appointment values to the variables declared in the first code section and in the appropriate format.
Using String.format fill in the Appoinment skeleton created earlier with the variable values.
Now that we have the ICalendar file created, we need to write it to the disk so as to attach it to the outgoing email
(Anybody knows a method of just creating the ICalendar file in memory and directly attaching the file without creating a Physical file please leave a feedback on how to do that,didnt get much time to look into it.)
Now that we have the ICalendar all we need to do is send a mail to the persons involved in the Appoinment with the ICalendar file as an attachment.
When the Person who receives the mail opens the attached ICalendar file,it will open up in Outlook as an Outlook Appoinment.
Cheers!!
P.S There are a lot of improvements that can be done in this code like for e.g using stringbuilder etc but I have skipped on them as this is just a code snippet.
To see the code for the above SendAppoinment method click here
*Update : For those of you interested in sending a Meeting Request here is the VCalendar format
* Known Issue: If a person rejects the meeting it wont be conveyed to you
I am going to show the method of sending an appointment by creating a ICalendar Appointment file first and then sending it as an attachement over email. (The way in which ICalendar format files are sent when you click on Tools ->Export -> as ICalendar file in the appointment )
Here is the declaration for the TimeFormat and some variables used to fill the Appoinment details. viz start time,endtime etc.
| const string c_strTimeFormat = "yyyyMMdd\\THHmmss\\Z"; string strStartTime=""; string strEndTime=""; string strTimeStamp=""; string strTempStartTime =""; string strTempEndTime = ""; string vCalendarFile = ""; |
Create a Skeleton for the appointment ICalendar file format and using the variables created above we will assign the values accordingly into the Appoinment skeleton. (This string concatation can be optimised by using string builder)
| // VCalendar Format. const string VCAL_FILE = "BEGIN:VCALENDAR\n" + "VERSION:1.0\n" + "BEGIN:VEVENT\n" + "DTSTART{0}\n" + "DTEND{1}\n" + "LOCATION;ENCODING=QUOTED-PRINTABLE:{2}\n" + "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:{3}\n" + "SUMMARY;ENCODING=QUOTED-PRINTABLE:{4}\n" + "TRIGGER:-PT15M\n" + "PRIORITY:3\n" + "END:VEVENT\n" + "END:VCALENDAR" ; |
Assign the Appointment values to the variables declared in the first code section and in the appropriate format.
DateTime dtmStartDate = DateTime.Parse(startDate.ToString()); DateTime dtmStartTime = DateTime.Parse(startDate + " " + startTime.ToString()); DateTime dtmEndTime = DateTime.Parse(startDate + " " + endTime.ToString()); strTempStartTime = string.Format("{0} {1}", dtmStartDate.ToShortDateString(),dtmStartTime.ToLongTimeString()); strTempEndTime = string.Format("{0} {1}", dtmStartDate.ToShortDateString(),dtmEndTime.ToLongTimeString()); strTimeStamp = (DateTime.Parse(strTempStartTime)).ToUniversalTime().ToString(c_strTimeFormat); strStartTime = string.Format(":{0}", strTimeStamp); strEndTime = string.Format(":{0}", (DateTime.Parse(strTempEndTime)).ToUniversalTime().ToString(c_strTimeFormat)); |
Using String.format fill in the Appoinment skeleton created earlier with the variable values.
| vCalendarFile = String.Format(VCAL_FILE, strStartTime, strEndTime, location, summary, subject , strTimeStamp, attendeeEmail.Trim() , "ShaunakP", attendeeName.Trim(), attendeeEmail.Trim(), organizerEmail.Trim()); |
Now that we have the ICalendar file created, we need to write it to the disk so as to attach it to the outgoing email
(Anybody knows a method of just creating the ICalendar file in memory and directly attaching the file without creating a Physical file please leave a feedback on how to do that,didnt get much time to look into it.)
filePath += "\\" + subject+ ".ics"; TextWriter tw = new StreamWriter(filePath); // write a line of text to the file tw.WriteLine(vCalendarFile.ToString()); // close the stream tw.Close(); |
Now that we have the ICalendar all we need to do is send a mail to the persons involved in the Appoinment with the ICalendar file as an attachment.
// Create object for sending mails MailMessage mail = new MailMessage(); mail.To = attendeeEmail.Trim(); mail.From = organizerEmail.Trim(); mail.Subject = "You have got a Appointment."; // create the attachment MailAttachment attachment = new MailAttachment(filePath, MailEncoding.UUEncode); // Attach mail.Attachments.Add( attachment ); SmtpMail.SmtpServer = _smtpServer; SmtpMail.Send( mail ); |
When the Person who receives the mail opens the attached ICalendar file,it will open up in Outlook as an Outlook Appoinment.
Cheers!!
P.S There are a lot of improvements that can be done in this code like for e.g using stringbuilder etc but I have skipped on them as this is just a code snippet.
To see the code for the above SendAppoinment method click here
*Update : For those of you interested in sending a Meeting Request here is the VCalendar format
| "BEGIN:VCALENDAR\n" + "PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN\n" + "VERSION:2.0\n" + "METHOD:REQUEST\n" + "BEGIN:VEVENT\n" + "ATTENDEE;CN=\"{8}\";ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:{9}\n\n" + "ORGANIZER:MAILTO:{10}\n" + "DTSTART{0}\n" + "DTEND{1}\n" + "LOCATION:{2}\n" + "TRANSP:OPAQUE\n" + "SEQUENCE:0\n" + "UID:{6}\n" + "DTSTAMP:{5}\n" + "DESCRIPTION:{3}\n" + "SUMMARY:{4}\n" + "PRIORITY:5\n" + "X-MICROSOFT-CDO-IMPORTANCE:1 \n" + "CLASS:PUBLIC\n" + "BEGIN:VALARM\n" + "TRIGGER:-PT15M\n" + "ACTION:DISPLAY\n" + "DESCRIPTION:Reminder\n" + "END:VALARM\n" + "END:VEVENT\n" + "END:VCALENDAR" ; |
* Known Issue: If a person rejects the meeting it wont be conveyed to you