我可以使用C#
和System.Net.Mail.SmtpClient
发送电子邮件,并使用Gmail作为我的smtp服务器。现在,我想在电子邮件中添加schema标记。我希望看到它们会影响电子邮件在Google产品中的显示方式。我已按照here的说明进行操作,但未成功。我正在使用application/ld+json
嵌入标记。
这是我生成电子邮件的方式:
public static Task Main()
{
var mail = new MailMessage("[email protected]", "[email protected]");
var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "my password");
mail.Subject = "fight confirmation";
mail.IsBodyHtml = true;
mail.Body = GetMailBody ();
await client.SendMailAsync (mail);
}
这是我的GetMailBody()
方法:
public static string GetMailBody ()
{
return @"
<html>
<body>
<script type=""application/ld+json"">
{
""@context"": ""http://schema.org"",
""@type"": ""EmailMessage"",
""potentialAction"": {
""@type"": ""ConfirmAction"",
""name"": ""Approve Expense"",
""handler"": {
""@type"": ""HttpActionHandler"",
""url"": ""https://myexpenses.com/approve?expenseId=abc123""
}
},
""description"": ""Approval request for John's $10.13 expense for office supplies""
}
</script>
Let me know if you're in.
</body>
</html>
";
}
我希望在收件箱中看到一个按钮,类似于Google文档中显示的按钮:
但是我的收件箱中的所有内容都与没有任何schema标记的情况相同。
我花了几个小时来测试您的模式和代码。
通常,您的代码中没有问题,但是未显示这些按钮的主要原因是,据我从其他资源了解到,这要求您使用DKIM或SPF对您自己的电子邮件进行签名,以使Gmail能够渲染动作。
检查此answer,此answer和此answer都存在相同的问题,并提到相同的解决方案。
此外,当您将电子邮件列入白名单时,我将留下一些工具和资源链接,这些链接将帮助您验证架构数据并继续进行下去:
最后,这可能与您的问题无关,但值得一提的是,这些操作按钮仅是为Gmail所有者而不是其他电子邮件客户端提供的,这意味着如果您正在寻找更通用的方式来在您的邮件中显示按钮电子邮件,也许只是普通的html就能解决问题,这是一个简单的示例:
public static string GetMailBody()
{
return @"
<table width=""100%"" cellspacing=""0"" cellpadding=""0"">
<tr>
<td>Approval request for John's $10.13 expense for office supplies</td>
</tr>
<tr>
<td>
<table cellspacing=""0"" cellpadding=""0"">
<tr>
<td style=""border-radius: 2px;"" bgcolor=""#ED2939"">
<a href=""https://www.yourWebConfirmationLink.com"" target=""_blank"" style=""padding: 8px 12px; border: 1px solid #ED2939;border-radius: 2px;font-family: Helvetica, Arial, sans-serif;font-size: 14px; color: #ffffff;text-decoration: none;font-weight:bold;display: inline-block;"">
Confirm Your Order
</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
";
}
这将给: