title: How to substitute Sendgrid template variables? (in C#)
body: <p>I defined a variable <code>&lt;%datetime%&gt;</code> in my SendGrid template. I decided by this naming convention to follow the already placed <code>&lt;%subject%&gt;</code> of the subject line. I see different variable naming conventions in the examples: <a href="https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L41" rel="nofollow noreferrer">https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L41</a> uses <code>-name-</code> and <code>-city-</code>, while <a href="https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L157" rel="nofollow noreferrer">https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L157</a> uses <code>%name%</code> and <code>%city%</code>.</p>
  
  <p>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.</p>
  
  <pre><code>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("&lt;%subject%&gt;", $"Your Report on {shortDateTimeStr}");
  mail.Personalization[0].AddSubstitution("&lt;%datetime%&gt;", longDateTimeStr);
  // Some code adds several attachments here
  
  var response = await sendGrid.client.mail.send.post(requestBody: mail.Get());
  </code></pre>
  
  <p>The request is accepted and processed, but email I get still have the subject line </p>
  
  <blockquote>
    <p>"Supposed to be replaced. Can I get rid of this somehow then?"</p>
  </blockquote>
  
  <p>The body is replaced by the raw template content but variable is not substituted either. What am I doing wrong?</p>