1

<%datetime%>SendGrid テンプレートで変数を定義しました。この命名規則により、既に配置され<%subject%>ている件名に従うことにしました。例にはさまざまな変数の命名規則があります: https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L41-name-and-city-を使用します/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());

リクエストは受け入れられて処理されましたが、受信したメールにはまだ件名が含まれています

「交換のはずです。では、これをどうにか取り除くことはできますか?」

本文は未加工のテンプレート コンテンツに置き換えられますが、変数も置き換えられません。私は何を間違っていますか?

4

1 に答える 1

5

How to Add Custom variables to SendGrid email via API C# および Templateの質問と回答を読んだ後、<%foobar%>型表記を使用するのは間違った決定であることに気付きました。

基本的にそれは SendGrid 独自の表記法であり、<%subject%>あなたが に割り当てたものを置き換えることを意味します。Mail subject私の場合は"Supposed to be replaced. Can I get rid of this somehow then?". 今、私はそこに適切な主題を組み立てます。

{{foobar}}テンプレート本体自体で、変数の表記に切り替えました。上記のリンクの質問に対する最後の回答では<%body%>、テンプレートの本文に挿入する必要があると記載されていますが、必須ではありません。それがなくても動作します。{{foobar}}の代わりに適切な置換を使用して、件名行に独自の変数を使用できると思います<%subject%>

基本的に、テンプレートのデフォルトの状態は<%subject%>件名と<%body%>本文です。これにより、置換が必要なく、API を介して件名と本文を指定した場合、シームレスな電子メール配信が実現します。

間違っていたら訂正してください。

string subject = $"Report on ${shortDateTimeStr}";
string emaiTo = ConfigurationManager.AppSettings["EmailTo"].ToString();
Email to = new Email(emaiTo);
Content content = new Content("text/html", "Placeholder");
Mail mail = new Mail(from, subject, to, content);
mail.TemplateId = "AC6A01BB-CFDF-45A7-BA53-8ECC54FD89DD";
mail.Personalization[0].AddSubstitution("{{datetime}}", longDateTimeStr);

TL;DR: 独自の変数に記法を使用しない<%foobar%>でください。他の多数のスタイルから 1 つを選択してください。私が読んだ例やドキュメントのどれも、これについて言及していません。

于 2017-01-03T02:02:06.973 に答える