Pages

Sunday, September 5, 2010

Create Outlook VCalendar reminder file and attach it into an email using C#.net

Using the below code example you can create Outlook calendar file with extention .ics. Then, using System.Net.Mail you can attach it into an email as binary data.

// CreateAppointment creates email attachment containing outlook calendar file.
// Attachment representing a calendar appointment

string AppointmentFileName = "Your file name";
string AppointmentSummary = "Your appointment summary";
string AppointmentDescription = "Your appointment description";
int AppointmentDuration = 20;            // Choose appropriate time duration
private System.Net.Mail.Attachment CreateAppointment()
{

// Building .ics file string

string filestring = "BEGIN:VCALENDAR" + Environment.NewLine +
                         "VERSION:1.0" + Environment.NewLine +
                         "BEGIN:VEVENT" + Environment.NewLine +
                         "DTSTART:" + DateTime.Parse(TextBoxDate.Text).ToString("s") + Environment.NewLine +
                         "DTEND:" + DateTime.Parse(TextBoxDate.Text).AddMinutes                                            (AppointmentDuration).ToString("s")
                         +  Environment.NewLine +
                         "SUMMARY:" + AppointmentSummary + Environment.NewLine +
                         "DESCRIPTION:" + AppointmentDescription + Environment.NewLine +
                         "END:VEVENT" + Environment.NewLine +
                         "END:VCALENDAR";

// Convert filestring into binary.

byte[] filedata = Encoding.UTF8.GetBytes(filestring);
MemoryStream memorystream = new MemoryStream(filedata);


System.Net.Mail.Attachment appointment = new System.Net.Mail.Attachment(memorystream, AppointmentFilename, "text/calendar");


return appointment;
}

2 comments:

Siva Sottallu said...

Hi Sahan,

Thanks for the code. I having some issue with the program. Everything goes fine except that on my outlook I get the ICS file attached with "not supported calendar message.ics", even though i have given a different name.

Any idea how to fix this. I am using Outlook 2007 and out host is Exchange 2007.

I appreciate any help in this.

Thanks again.
Siva

Massapon said...

I have a problem with the DESCRIPTION Field.
I have a VCalendar object that I create in this way:

var str = new StringBuilder();
str.AppendLine("BEGIN:VCALENDAR");
str.AppendLine("PRODID:-//Test123");
str.AppendLine("VERSION:2.0");
str.AppendLine("METHOD:REQUEST");

str.AppendLine("BEGIN:VEVENT");
str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", startTime.ToUniversalTime()));
str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endTime.ToUniversalTime()));
str.AppendLine(string.Format("LOCATION: {0}", location));
str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
str.AppendLine(string.Format("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:{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");

In the description field I have some lines.
If a use Environment.NewLine or "\r\n" I see this field correctly on Android phone but on Black berry I see only the first line.
If I use =OD=0A this works on Black Berry but in Android I see =0D=0A instead of new line...

Can you help me please?

Thanks in advance!