<%datetime%>
我在我的 SendGrid 模板中定义了一个变量。我决定按照这个命名约定遵循已经放置<%subject%>
的主题行。我在示例中看到了不同的变量命名约定:https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L41使用-name-
and -city-
,而https://github.com/sendgrid /sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L157使用%name%
和%city%
.
我只是假设,变量替换基于简单的模式匹配,因此这些示例的对应模板包含完全相同的字符串。到目前为止,无论出于何种原因,这对我来说都不起作用。
string sendGridApiKey = ConfigurationManager.AppSettings["SendGridApiKey"].ToString();
var sendGrid = new SendGridAPIClient(sendGridApiKey);
string emailFrom = ConfigurationManager.AppSettings["EmailFrom"].ToString();
Email from = new Email(emailFrom);
string subject = "Supposed to be replaced. Can I get rid of this somehow then?";
string emaiTo = ConfigurationManager.AppSettings["EmailTo"].ToString();
Email to = new Email(emaiTo);
Content content = new Content("text/html", "Supposed to be replaced by the template. Can I get rid of this somehow then?");
Mail mail = new Mail(from, subject, to, content);
mail.TemplateId = "AC6A01BB-CFDF-45A7-BA53-8ECC54FD89DD";
mail.Personalization[0].AddSubstitution("<%subject%>", $"Your Report on {shortDateTimeStr}");
mail.Personalization[0].AddSubstitution("<%datetime%>", longDateTimeStr);
// Some code adds several attachments here
var response = await sendGrid.client.mail.send.post(requestBody: mail.Get());
请求已被接受并处理,但我收到的电子邮件仍然有主题行
“应该是被替换了。那我能不能把它去掉?”
正文被原始模板内容替换,但变量也未被替换。我究竟做错了什么?