Saturday, March 12, 2011

Outlook Calender email as an attachment

Microsoft outlook user can use the advantage of creating meeting/appointment calender or scheduler. But if you have to send the calender appointment as an attachment, so that the receiver can have the option to set meeting calender on his own outlook client then you have to create an .ics file.
Today I will briefly discuss about how to create an ics file and send it to the client.

Step 1: Create the content of an .ics file.
string meetingLocation = "My Place.";
string meetingSubject = "31st Night party";
string meetingDescription = "Celebration of new year.";
System.DateTime meetingBeginDate = Convert.ToDateTime("31/12/2011 10:00:00 PM");
System.DateTime meetingEndDate = Convert.ToDateTime("1/1/2012 11:00:00 AM");
String[] contents = { "BEGIN:VCALENDAR", "PRODID:-//Flo Inc.//FloSoft//EN",
                              "BEGIN:VEVENT","DTSTART:" 
+ meetingBeginDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"),"DTEND:" 
+ meetingEndDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"), "LOCATION:" 
+ meetingLocation, "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + meetingDescription,
"SUMMARY:" + meetingSubject, "PRIORITY:3", "END:VEVENT", "END:VCALENDAR" };

Step 2: Save it to server machine[could be in temp document folder] as an ics file.

System.IO.File.WriteAllLines(Server.MapPath("test.ics"), contents);

Step 3: Attach the file into email as an email attachment.

Attachment mailAttachment = new Attachment(Server.MapPath("test.ics"));
yourMailMessage.Attachments.Add(mailAttachment);

Where yourMailMessage is an instance object of type System.Net.Mail.MailMessage .

Now if the mail is sent ,the receiver will get an appointment file as an attachment and just by double clicking on that attached file user can add meeting to his outlook client.


No comments:

Post a Comment

Rest Service using WEB API C#

This post will show a simple example of REST service built on Micrsoft WEB API framework. WEB API is an extensible framework from microsof...