To create an event that can be recreated across multiple calendars like Google and Apple Calendar, you can use iCalendar files (*. ics).
There is the iCal.NET, an open source . NET library for file creation and manipulation in this pattern. It is available as nuget pack, just run
Install-Package Ical.Net
no Package Manager do Visual Studio.
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 above example you see how to use this library to generate the iCalendar file, create an email, and attach it.
The *.ics generated file follows this pattern (removed 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 great gain in using this type of file is that it is accepted in many providers. Yahoo, Apple, Google and Microsoft accept this standard.
See how Outlook Web treats when there is an attached *.ics:
Thank you for the reply @vnbrs! Tell me something, when the person receives this email, will they be asked to enter this commitment in the agenda? or will be automatically included?
– Rogerio Azevedo
This is the responsibility of the email client. To my knowledge, Windows 10 Mail and Outlook Web make a request to insert.
– vinibrsl
I will do some tests, generating a file of this and send it to my gmail email to see the behavior. Thank you very much!
– Rogerio Azevedo