To create an event that can be recreated across calendars such as Google and Apple Calendar, you can use iCalendar (* .ics) files.
There is iCal.NET, an open source .NET library for creating and manipulating files in this standard. It is available as NuGet package , just run
Install-Package Ical.Net
in the Visual Studio Package Manager.
MailMessage message = new MailMessage();
message.Subject = "Seu compromisso";
message.Body = "Adicione ao seu calendário!";
message.To.Add("[email protected]");
message.From = new MailAddress("[email protected]", "Fulano de Tal");
// criação do evento
calendar.Events.Add(new Event {
Class = "PUBLIC",
Summary = "Seu evento",
Created = new CalDateTime(DateTime.Now),
Description = res.Details,
Start = new CalDateTime(Convert.ToDateTime(DateTime.Now)),
End = new CalDateTime(Convert.ToDateTime(DateTime.Now.AddDays(5))),
Sequence = 0,
Uid = Guid.NewGuid().ToString(),
});
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, "evento.ics", "text/calendar");
message.Attachments.Add(attachment);
In the example above, you can see how to use this library to generate the iCalendar file, create an email, and attach it.
The generated * .ics file follows this pattern (removed from from here ):
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:[email protected]
DTSTAMP:19970714T170000Z
ORGANIZER;CN=John Doe:MAILTO:[email protected]
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
END:VEVENT
END:VCALENDAR
I reiterate that the big gain in using this type of file is that it is accepted by many providers. Yahoo, Apple, Google and Microsoft accept this standard.
See how Outlook Web handles when there is a * .ics attached: