Send email to create commitment in Google Calendar

Asked

Viewed 907 times

1

I have a C# ASP.NET MVC application, in which the user can schedule an appointment. In this application he chooses a text for the commitment, a Date to happen and also chooses, the sooner he wants to be notified of the commitment, which will generate a datetime for that date of the reminder.

The next step would be to create a "tool" that was consulted every 5 minutes if there are reminders to be sent to the user’s email. From what I read, I’d have to create a server bat to do this job.

The other option would be, in creating the commitment, send an email to the user, so that this email generates an automatic agenda for the user in the Google calendar, as happens today with airlines like TAM and AZUL. (All users have Google email).

Does anyone know how these emails work that generate commitments in Google? What suggest me?

1 answer

1


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:

inserir a descrição da imagem aqui

  • 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?

  • This is the responsibility of the email client. To my knowledge, Windows 10 Mail and Outlook Web make a request to insert.

  • 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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.