Sendgrid c#模板替换

时间:2016-07-07 16:08:22

标签: c# sendgrid

我无法让单词替换与c#中的sendgrid v3 api一致。有时标签会被替换,有时则不会。我不知道是什么导致了这一点。任何人都可以在我的代码中看到任何明显的错误吗?

            String apiKey = "KEY";
            dynamic sg = new SendGridAPIClient(apiKey);

            Email from = new Email("info@example.com");
            String subject = "Hello World from the SendGrid CSharp Library";
            Email to = new Email("example@gmail.com");
            Content content = new Content("text/html", " ");
            to.Name = "Joe";

            Mail mail = new Mail(from, subject, to, content);
            mail.TemplateId = "dfea45f3-d608-4860-9f38-c7d444qwrqwc1f";

            Personalization subs = new Personalization();
            subs.AddTo(to);
            subs.AddSubstitution("*|url|*", "http://asdasdasd.com");
            subs.AddSubstitution("*|username|*", "MrUsername");

            mail.AddPersonalization(subs);

            dynamic response = sg.client.mail.send.post(requestBody: mail.Get());

2 个答案:

答案 0 :(得分:1)

删除个性化并添加以下内容

mail.Personalization[0].AddSubstitution("*|url|*", "http://asdasdasd.com");
mail.Personalization[0].AddSubstitution("*|username|*", "MrUsername");

see code samples here

答案 1 :(得分:0)

我发现了我的问题。我认为你的是一样的。当您在发送之前查看邮件对象时,您会发现阵列中有2个拟人化项目。您执行此操作subs.AddTo(to);然后在mail.AddPersonalization(subs);这将在个性化阵列中创建2封电子邮件 - 我的错误有效负载示例为:

{
    "from": {
        "email": "no-reply@chromasports.com"
    },
    "subject": "",
    "personalizations": 
    [
        {
        "to": [{
            "email": "email@gmail.com"
        }]
        },
        {
        "to": [{
            "email": "email@gmail.com"
        }],
        "substitutions": {
            ":token": "http://alabala.com/auth/reset-password#token=or514rqHTeLjtjlN6WRppOu53yJJ64nSzcK86GF6Ite2BaZRa58YPMfTmM0wzQs4tMLbHy8YlpieDVBae1aD99TKnMh7wYNOE2nmu8gWePQoZiWhbFLomVBvApHA1fuxIxQ1elui2QXAmGPtwDdvVOgvAiSF3HQteuvFwP5kXUnXXEeddYLIUHqJCDrATiOsSgxvcpKmhXhrhx78ns49f4hakGlLMncNgBuMGmL3wCduY9f22hjCs9tbIPq5h5V"
        }
        }
    ],
    "content": [{
        "type": "text/html",
        "value": "\u003chtml\u003e\u003cbody\u003eHTML content\u003c/body\u003e\u003c/html\u003e"
    }],
    "template_id": "unique id"
}

” 检查您的有效负载并修复它尝试mail.AddPersonalization[0] = subs;希望这可以解决您的问题