Search code examples
c#outlookgmailicalendar

Generate and send iCal event to outlook


Im using DDay.iCal (1.0.2.575, from nuget) to generate a email event. Im using the following code to generate the event:

iCalendar iCal = new iCalendar
{
    Method = "PUBLISH",
    Version = "2.0"
};
//REQUEST for update
Event calendarEvent = iCal.Create<Event>();
calendarEvent.Summary = "Test";
calendarEvent.Start = new iCalDateTime(new DateTime(2015, 7, 1, 10, 0, 0));
calendarEvent.Duration = new TimeSpan(0, 45, 0);
calendarEvent.Description = "Test";
calendarEvent.Location = "Test";
calendarEvent.IsAllDay = false;
calendarEvent.UID = Guid.NewGuid().ToString();
calendarEvent.Organizer = new Organizer("[email protected]");

iCalendarSerializer serializer = new iCalendarSerializer(iCal);
string eventCode = serializer.SerializeToString(calendarEvent);

and then im using the following code to send it

SmtpClient smtpClient = new SmtpClient("smtp.someaddress.com");
MailMessage message = new MailMessage
{
    From = new MailAddress("[email protected]", "Someone"),
    Subject = "Test",
    Body = "Test"
};
System.Net.Mail.Attachment attachment = System.Net.Mail.Attachment.CreateAttachmentFromString(eventCode, "test.ics");
attachment.ContentType = new System.Net.Mime.ContentType("text/calendar");
attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
message.CC.Add(new MailAddress("[email protected]"));
message.Attachments.Add(attachment);
smtpClient.Send(message);

and the output is

BEGIN:VEVENT
DESCRIPTION:Test
DTEND:20150701T104500
DTSTAMP:20150605T082404Z
DTSTART:20150701T100000
DURATION:PT45M
LOCATION:Test
ORGANIZER:mailto:[email protected]
SEQUENCE:0
SUMMARY:Test
UID:dd175008-a4be-4fcf-a153-baff29ef5b8a
END:VEVENT

But outlook 2010 just says The operation failed. and gmail shows a calendar symbol but nothing happens if i click it, also if i click the attached .ics file it dosen´t know what to do with it..

What am i missing to make this work?


Solution

  • I found the error change the line string eventCode = serializer.SerializeToString(calendarEvent); to string eventCode = serializer.SerializeToString(iCal); and it works!