猿问

C#客户端如何将替代变量传递给sendgrid营销模板?

有一个预定义的营销模板,模板的相关部分如下所示:

嗨 [%first_name | 亲爱的粉丝%],xxxxxx

sendgrid 模板变量替换有很多不同的语法,这非常令人困惑。例如,我见过 [%variable_name%]、{{variable_name}}、-variable_name- 和 <%variable_name%>

如何将替换/个性化/模板数据(只是列出我见过的似乎指代同一事物的不同名称)从 C# 客户端传递到模板?



函数式编程
浏览 70回答 1
1回答

精慕HU

我假设您使用官方的 SendGrid Nuget 包。在这种情况下,您可以使用MailHelper类及其方法CreateSingleTemplateEmail,CreateSingleTemplateEmailToMultipleRecipients或者CreateMultipleTemplateEmailsToMultipleRecipients根据您的需要。在最简单的情况下,它应该是这样的。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var client = new SendGridClient(ApiKey);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var dynamicTemplateData = new Dictionary<string, string>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {"first_name", "John"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {"last_name", "Snow"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var msg = MailHelper.CreateSingleTemplateEmail(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new EmailAddress("from@test.com"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new EmailAddress("to@test.com"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "d-template-id",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dynamicTemplateData);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var response = await client.SendEmailAsync(msg);上述方法确实适用于事务模板,我对营销模板不是 100% 确定的。
随时随地看视频慕课网APP
我要回答