2016-07-07 63 views
0

我无法使用c#中的sendgrid v3 api一致地使用单词替换工作。有时标签会被替换,其他时候则不会。我对此感到不知所措。任何人都可以在我的代码中看到明显的错误?Sendgrid c#模板替换

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

      Email from = new Email("[email protected]"); 
      String subject = "Hello World from the SendGrid CSharp Library"; 
      Email to = new Email("[email protected]"); 
      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()); 
+0

嗨,你能解决这个问题吗?感谢您的答复!有同样的问题 –

+0

不,我发电子邮件sendgrid,但没有回应。 – loveforfire33

回答

0

我发现问题在我身边。我认为你是一样的。在发送邮件之前查看邮件对象时,您会发现阵列中有2个拟人化项目。你这样做subs.AddTo(to);再后来就mail.AddPersonalization(subs);这个性化阵列中创建2电子邮件 - 我错了有效载荷的例子是:

{ 
    "from": { 
     "email": "[email protected]" 
    }, 
    "subject": "", 
    "personalizations": 
    [ 
     { 
     "to": [{ 
      "email": "[email protected]" 
     }] 
     }, 
     { 
     "to": [{ 
      "email": "[email protected]" 
     }], 
     "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;希望这能解决您的问题

+0

或不使用Mail mail = new Mail(from,subject,to,content);并添加个性化和以后 –

1

卸下个性化并添加以下

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

see code samples here