我在SendGrid模板中定義了一個變量<%datetime%>
。我根據這個命名慣例決定遵循已經放置的主題行<%subject%>
。我在示例中看到不同的變量命名約定:https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L41使用-name-
和-city-
,而https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L157使用%name%
和%city%
。如何替換Sendgrid模板變量? (在C#中)
我只是假設變量替換是基於簡單模式匹配,因此這些示例的對應模板包含相同的確切字符串。儘管如此,無論出於何種原因,這對我來說都不起作用。
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());
的請求被接受和處理,但是郵件我得到仍然有主題行
「應該被替換。我可以擺脫這個莫名其妙呢?」
正文被原始模板內容替換,但變量也未被替換。我究竟做錯了什麼?