I defined a variable <%datetime%>
in my SendGrid template. I decided by this naming convention to follow the already placed <%subject%>
of the subject line. I see different variable naming conventions in the examples: https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L41 uses -name-
and -city-
, while https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L157 uses %name%
and %city%
.
I just assume, that the variable substitution is based on simple pattern matching, so the counterpart templates of those examples contain the same exact strings. That doesn't work for me so far though for whatever reason.
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());
The request is accepted and processed, but email I get still have the subject line
"Supposed to be replaced. Can I get rid of this somehow then?"
The body is replaced by the raw template content but variable is not substituted either. What am I doing wrong?