How to send an email in Gmail such that an event gets created automatically in Google Calendar
Asked Answered
M

0

7

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:

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);
        }
    }
}
Malcolm answered 18/10, 2021 at 10:16 Comment(4)
we can send the email but event is not been created yet where exactly are you sending the event. The email body looks like it just contains "Body"Hernia
As @DaImTo stated, is it possible to have the part of your code that creates/attaches the event?Flutterboard
@DaImTo I have added the email body and the code which is using it, please take a look when you get timeMalcolm
I see you are using different email users in the first snippet of code and the same user "[email protected]" in the last piece of code, that could be an issue unless you've registered the domain. Which .NET version are you using?Flutterboard

© 2022 - 2024 — McMap. All rights reserved.