【问题标题】:Sendgrid c# Template SubstitutionSendgrid c#模板替换
【发布时间】:2016-11-10 01:26:38
【问题描述】:

我无法让单词替换与 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());

【问题讨论】:

  • 您好,请问您的问题解决了吗?感谢您的答复!遇到同样的问题
  • 不,我给 sendgrid 发了邮件,但还没有回复。

标签: c# sendgrid


【解决方案1】:

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

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

see code samples here

【讨论】:

    【解决方案2】:

    我发现了我身边的问题。我想你的也是一样的。当您在发送前查看邮件对象时,您会发现数组中有 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;希望这能解决你的问题

    【讨论】:

    • 或者不使用Mail mail = new Mail(from, subject, to, content);并将个性化添加到以后
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    相关资源
    最近更新 更多