I have a requirement where we need to send an email such that an event gets created automatically in the Google Calendar of the recipient's account.
Requirements:
- Need to send an email after confirmation
- The email should be such that Google accepts it as an event
Assumption:
- Assuming that the confirmation email will have some event related details such as:
- Location
- StartTime
- Event name, etc
Approach:
- Google provides a way of doing it
- Documentation link: https://developers.google.com/gmail/markup/overview#google_calendar
- Basically, we need to have our email HTML in a specific format
- Here we need to use events: https://developers.google.com/gmail/markup/reference/event-reservation
- So as of now, I have a small script that sends an email in the specified format, but there is no event being created
- It also says that this functionality should work when we have both the sender and the receiver's email address as the same (for testing purposes)
- For production use, we need to follow a bunch of steps to set the functionality
- Documentation link: https://developers.google.com/gmail/markup/registering-with-google
The pending issue is that – we can send the email but the event has not been created yet (even after having the email HTML in the specified format). Can someone please tell me how to send the email so that an event gets created automatically in Google Calendar?
Note:
- To send the email, I have used something like this in C#
- First I turn on Less secure app access for the sender's email. Then, I use the following code.
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Here is the HTML of my email.
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<p>
Dear John, thanks for booking your Google I/O ticket with us.
</p>
<p>
BOOKING DETAILS<br />
Order for: Hasnu Zama<br />
Event: Google I/O 2013<br />
When: Oct 18th 2021 19:00pm IST<br />
Venue: Moscone Center, 800 Howard St., San Francisco, CA 94103<br />
Reservation number: IO12345<br />
</p>
<!-- JSON-LD markup generated by Google Structured Data Markup Helper. -->
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "EventReservation",
"reservationNumber": "E123456789",
"reservationStatus": "http://schema.org/Confirmed",
"underName": {
"@type": "Person",
"name": "John Smith"
},
"reservationFor": {
"@type": "Event",
"name": "Foo Fighters Concert",
"startDate": "2021-10-18T19:00:00-05:30",
"location": {
"@type": "Place",
"name": "AT&T Park",
"address": {
"@type": "PostalAddress",
"streetAddress": "24 Willie Mays Plaza",
"addressLocality": "San Francisco",
"addressRegion": "CA",
"postalCode": "94107",
"addressCountry": "US"
}
}
}
}
</script>
</body>
</html>
Here is the JSON-LD that should have worked according to the examples in the doc, not sure why it is not working.
This is stored in a file, and then I am reading this file and pass the content as a parameter for the body.
using System;
using System.IO;
namespace CreateEventFromEmail
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string fromEmailId = "[email protected]";
string toEmailId = "[email protected]";
CreateAndSendEmail createAndSendEmail = new CreateAndSendEmail(fromEmailId, toEmailId);
string body = File.ReadAllText(@"D:\office-work\add-events-from-email-POC\CreateEventFromEmail\EmailTemplates\EventDetails.html");
createAndSendEmail.SendEmail(body);
}
}
}