You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Gmail发送可自动在收件人Google Calendar创建事件的邮件

邮件触发Google Calendar自动创建事件问题及解决方案

需求背景

  • 需要在用户完成确认操作后发送邮件,邮件可被Gmail识别为有效事件,自动在收件人Google Calendar中创建对应日程
  • 确认邮件已包含地点、开始时间、事件名称等所有必要信息

现有实现问题

按照官方规范编写的带JSON-LD标记的邮件发送成功后,未触发自动创建日历事件逻辑,测试场景下收发双方为同一账号也未生效。

补充背景信息

现有发信代码(C#)

using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "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);
}

现有邮件HTML内容

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

现有主程序代码

using System;
using System.IO;

namespace CreateEventFromEmail
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string fromEmailId = "ronyrocx24@gmail.com";
            string toEmailId = "ronyrocx24@gmail.com";

            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);
        }
    }
}

修复方案

1. 开启邮件HTML解析配置

C#的MailMessage类默认将正文识别为纯文本,需要手动开启HTML解析配置,在初始化邮件对象时添加IsBodyHtml = true,修改后的发信核心代码如下:

using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body,
    IsBodyHtml = true // 新增该行配置
})
{
    smtp.Send(message);
}

2. 补全JSON-LD必填字段

当前的EventReservation结构缺少官方要求的modificationDate字段,需补充该字段,值为ISO 8601格式的时间戳,修改后的JSON-LD示例如下:

{
  "@context": "http://schema.org",
  "@type": "EventReservation",
  "reservationNumber": "E123456789",
  "reservationStatus": "http://schema.org/Confirmed",
  "modificationDate": "2021-10-10T12:00:00-05:30", // 新增该行
  "underName": {
    "@type": "Person",
    "name": "John Smith"
  },
  // 其余原有字段保持不变
}

3. 检查Gmail账号配置

测试时请确认收件账号的Gmail设置中已开启自动添加事件的权限:进入Gmail设置→常规→活动设置,勾选「自动将我收到的活动邀请添加到我的日历」选项。

4. 优化发信认证方式

Google已逐步停止支持低安全性应用访问权限,建议改用应用专用密码(仅支持开启了两步验证的普通谷歌账号)或者OAuth2认证方式发信,避免邮件被判定为垃圾邮件,跳过结构化标记解析流程。

5. 验证结构化数据合法性

可以使用Google官方提供的结构化数据测试工具,验证编写的JSON-LD标记是否存在语法错误或者遗漏字段。

内容的提问来源于stack exchange,提问作者Arghyadeep Ghosh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 19:39:02