简体   繁体   English

Sendgrid - 如何访问“收件人”电子邮件地址对象以在 v3 api C# 中个性化动态模板

[英]Sendgrid - How do I access the "to" email address object to personalize dynamic template in v3 api C#

We are using Sendgrid C# library created a stored dynamic template.我们使用 Sendgrid C# 库创建了一个存储动态模板。 It is working fine except we cannot figure out how to access the "to" object to display the user's name dynamically in the template.它工作正常,只是我们无法弄清楚如何访问“to”对象以在模板中动态显示用户名。 Here is the JSON being sent:这是正在发送的 JSON:

{
   "from":{
      "email":"fromemail@mydomain.com"
   },
   "personalizations":[
      {
         "to":[
            {
               "name":"test name",
               "email":"test@testingmail.com"
            }
         ],
         "dynamic_template_data":{
            "MailBody":"this is a \u003cstrong\u003eTEST!\u003c/strong\u003e",
            "MailSalutation":"Best Regards,",
            "MailGreeting":"Dear"
         }
     
      
      },{
         "to":[
            {
               "name":"another name",
               "email":"anothertest@testingmail.com"
            }
         ],
         "dynamic_template_data":{
            "MailBody":"this is a \u003cstrong\u003eTEST!\u003c/strong\u003e",
            "MailSalutation":"Best Regards,",
            "MailGreeting":"Dear"
         }
     
      
      }
   ],
   "template_id":"xxxxxxxxxx-ede3cd3f9b"
}

The template looks something like this:模板看起来像这样:

<p> {{MailGreeting}} {{to.name}} or {{to[0].name}}???,</p>
        
        <p>
        {{{MailBody}}}
        </p>
        <p>
        {{MailSalutation}}

</p>

The name property of the "to" object is what we want to display here {{to.name}} “to”对象的name属性就是我们要在这里显示的{{to.name}}

We are using the recommended method from the sample code with our data being fed in.我们正在使用示例代码中推荐的方法并输入我们的数据。

Our class object:我们的类对象:

public class EmailMergeSendDetails
        {
            public List<EmailAddress> MailToList { get; set; }
            public string MailFrom  { get; set; }
            public string MailFromName { get; set; }
            public string MailSubject { get; set; }
            public string MailBody { get; set; }
            
    }

Using Sendgrid C# to send使用 Sendgrid C# 发送

var from = new EmailAddress(d.MailFrom, d.MailFromName);
            var tos = d.MailToList;

                var dynamicTemplateData = new EmailMergeSendDetails
                {
                    Subject = d.MailSubject,
                    MailSalutation = d.MailSalutation,
                    MailGreeting = d.MailGreeting,
                    MailBody = d.MailBody,
                   
            };

                var msg = MailHelper.CreateSingleTemplateEmailToMultipleRecipients(from,
                                                                       tos,
                                                                       "d-xxxx-template-id-here",
                                                                       dynamicTemplateData
                                                                       );

var response = await client.SendEmailAsync(msg);

Why isn't this working for the {{to.name}}?为什么这不适用于 {{to.name}}? I'm guessing that the only data available for the {{}} is the dynamic_template_data?我猜 {{}} 唯一可用的数据是 dynamic_template_data? If so, how do we display the to.name.如果是这样,我们如何显示 to.name。 It feels like such a basic thing to be able to do but we've literally spend hours on it :).感觉这是一件很基本的事情,但我们确实花了几个小时做这件事:)。

For the sake of clarity, we have a template for a newsletter which stores dynamic template data in our database.为了清楚起见,我们有一个时事通讯模板,它在我们的数据库中存储动态模板数据。 We want to query that data along with multiple name/email addresses to send it to (25k+ of them), fill in the template with the newsletter data and send it to each one of the recipients adding a "Dear _____" at the top.我们想要查询该数据以及多个姓名/电子邮件地址以将其发送至(超过 25k 个),使用时事通讯数据填写模板并将其发送给在顶部添加“亲爱的 _____”的每个收件人。 Seems super basic.看起来超级基础。 How can we do this using the dynamic templates if CreateSingleTemplateEmailToMultipleRecipients is not the correct approach?如果 CreateSingleTemplateEmailToMultipleRecipients 不是正确的方法,我们如何使用动态模板来做到这一点?

I don't think you can.我不认为你可以。 Either way, To is an array, so {{to.name}} is not specific enough.无论哪种方式, To都是一个数组,因此{{to.name}}不够具体。

You can always add additional values to your dynamic_template_data though;不过,您始终可以向dynamic_template_data添加其他值; eg "RecipientName", which you would then set to "test name", and then reference it in your template as {{RecipientName}} .例如“RecipientName”,然后您将其设置为“test name”,然后在您的模板中将其引用为{{RecipientName}}

Or... the way you have done it, you could just add it to the end of MailGreeting ;或者...按照您的方式,您可以将其添加到MailGreeting eg give it a value of "Dear test name" instead of just "Dear".例如,给它一个“亲爱的测试名称”而不是“亲爱的”的值。

It's then implicit that you're going to specify only one recipient per email;这样就暗示您将只为每封电子邮件指定一个收件人; if this is not the case you'd need to perform multiple sends instead, with one recipient each.如果不是这种情况,您需要执行多个发送,每个发送一个收件人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用服务帐户通过.NET C#访问Google AnalyticsAPI V3? - How do I use a Service Account to Access the Google Analytics API V3 with .NET C#? 使用C#的SendGrid V3 API。 电子邮件未发送 - SendGrid V3 api with C#. email not sending 尝试从现有列表创建段时禁止访问 (Sendgrid v3 API C#) - Access forbidden when trying to create segment from an existing list (Sendgrid v3 API C#) 使用C#的SendGrid V3 API无法发送邮件 - SendGrid V3 api with C# not able to send mail 如何在 C# 中使用 Sendgrid (v3) 创建自定义字段定义? - How to create custom field definitions with Sendgrid (v3) in C#? 使用SendGrid v3标识将事务模板作为确认电子邮件发送 - Identity using SendGrid v3 to send transactional template as confirmation email 如何通过API C#和Template向SendGrid电子邮件添加自定义变量 - How to Add Custom variables to SendGrid email via API C# and Template HttpRequestException - SendGrid Email API v3 集成在 1&amp;1 Ionos 计划上 - HttpRequestException - SendGrid Email API v3 integration on a 1&1 Ionos plan SendGrid v3 API - 使用具有空/无内容的模板发送邮件 - SendGrid v3 API - Send Mail Using Template with Empty/No Content 如何使用sendgrid API C#获取电子邮件状态? - How to get email status using sendgrid api c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM