Create a calendar invite (ics file) in ASP NET

Creating a calendar invite (ics file) and attaching a email via service or sending an email with downloadable calendar invite is what we are going to learn in following posts.

In this post, I will show how you can use ICal library from Nuget and create a calendar invite.

The calendar begins with the keyword BEGIN:VCALENDAR and ends with END:VCALENDAR. In between the calendar you can add as many events as you want beginning each event with BEGIN:VEVENT and ending with END:VEVENT.

There is a catch when adding multiple events to a calendar, and that is the type of METHOD you use. With METHOD:PUBLISH, you can add multiple events and upon opening the iCal with MS Outlook it will create a new calendar, separate from your main Exchange calendar. METHOD:REQUEST creates a calendar invite, so you can only include one event and upon opening with MS Outlook it will ask you to accept the event and add it to your main Exchange calendar. With this method you have to include the organizer (ORGANIZER;CN=Your Name:mailto:email@company.com)

You create a new calendar, loop thru a list of events, serialize the calendar, convert to bytes, put into a memory stream and finally attach it to the email message. Here is the snippet to the same.

using Ical.Net;
using Ical.Net.DataTypes;
using Ical.Net.Serialization.iCalendar.Serializers;
using Ical.Net.Serialization;
...
...
...

MailMessage message = new MailMessage();
message.To.Add("naik899@gmail.com");
message.From = new MailAddress("info@company.com", "Company, Inc");
message.Subject = "subject";
message.Body = "emailbody";
message.IsBodyHtml = true;
...
...
...

var calendar = new Ical.Net.Calendar();

foreach (var res in reg.Reservations) {
        calendar.Events.Add(new Event {
                Class = "PUBLIC",
                Summary = res.Summary,
                Created = new CalDateTime(DateTime.Now),
                Description = res.Details,
                Start = new CalDateTime(Convert.ToDateTime(res.BeginDate)),
                End = new CalDateTime(Convert.ToDateTime(res.EndDate)),
                Sequence = 0,
                Uid = Guid.NewGuid().ToString(),
                Location = res.Location,
        });
}

var serializer = new CalendarSerializer(new SerializationContext());
var serializedCalendar = serializer.SerializeToString(calendar);
var bytesCalendar = Encoding.UTF8.GetBytes(serializedCalendar);
MemoryStream ms = new MemoryStream(bytesCalendar);
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(ms, "event.ics", "text/calendar");

message.Attachments.Add(attachment);
...
...
...

Here is how you can create a calendar invite (ics file) attach to email using C#.

Thanks for dropping by !!! Feel free to comment to this post or you can drop me an email at naik899@gmail.com.

The post Create a calendar invite (ics file) in ASP NET appeared first on Ravindra Naik.

Comments

Popular posts from this blog

Serve GZip CSS and JS from AWS S3