【问题标题】:Add Additional Substitution Sendgrid C#添加额外的替换 Sendgrid C#
【发布时间】:2018-02-23 01:15:36
【问题描述】:

我在为使用 Sendgrid C# 发送电子邮件添加额外替换时遇到问题。 https://github.com/sendgrid/sendgrid-csharp/ 例如,如何为 -name- 添加一个替代项? 我怎样才能做到这一点?感谢您的帮助。

这是我的代码,可用于一个替换(电子邮件)。

            var emails = group.ToList();
            List<string> subjects = new List<string>();
            var substitutions = new List<Dictionary<string, string>>();
            for (int i = 0; i < tos.Count; i++)
            {
                subjects.Add(subject);
                substitutions.Add(new Dictionary<string, string>() { { "-email-", tos[i].Email } });
            }
            string plainTextContent = null;
            string htmlContent = body;

            var msg2 = MailHelper.CreateMultipleEmailsToMultipleRecipients(from,
                                                          emails,
                                                          subjects,
                                                          plainTextContent,
                                                          htmlContent,
                                                          substitutions
                                                          );

【问题讨论】:

    标签: c# sendgrid


    【解决方案1】:

    sendgrid C# API v3.0 的动态事务电子邮件模板示例。

            var client = new SendGridClient(apiKey);
            var msg = new SendGridMessage();
            msg.SetFrom(new EmailAddress("test@example.com", "Example User"));
            msg.AddTo(new EmailAddress("test@example.com", "Example User"));
            msg.SetTemplateId("d-d42b0eea09964d1ab957c18986c01828");
    
            var dynamicTemplateData = new ExampleTemplateData
            {
                Subject = "Hi!",
                Name = "Example User"
            };
    
            msg.SetTemplateData(dynamicTemplateData);
            var response = await client.SendEmailAsync(msg);
    
        private class ExampleTemplateData
        {
            [JsonProperty("subject")]
            public string Subject { get; set; }
    
            [JsonProperty("name")]
            public string Name { get; set; }
        }
    

    模板正文:

    <html>
    <head>
        <title>{{subject}}</title>
    </head>
    <body>
    Hello {{name}},
    <br/><br/>
    I'm glad you are trying out the dynamic template feature!
    <br/><br/>
    Enjoy it
    <br/><br/>
    </body>
    </html>
    

    更多信息:https://github.com/sendgrid/sendgrid-csharp/blob/master/USE_CASES.md#transactional-templates

    【讨论】:

      【解决方案2】:

      示例: https://github.com/sendgrid/sendgrid-csharp/diffs/0?base_sha=f249c79dc01477f8bbb28ff32dbf34d2e1a0866b&commentable=true&head_user=yvesmh&pull_number=811&sha1=f249c79dc01477f8bbb28ff32dbf34d2e1a0866b&sha2=f5b769369fe197b8c1b90b0c306271ca5dd9cd92&short_path=6274ab0&unchanged=expanded&utf8=%E2%9C%93#send-a-single-email-with-multiple-substitutions

      using SendGrid;
      using SendGrid.Helpers.Mail;
      using System;
      using System.Threading.Tasks;
      using System.Collections.Generic;
      
      namespace Example
      {
      internal class Example
      {
          private static void Main()
          {
              Execute().Wait();
          }
      
          static async Task Execute()
          {
              var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
              var client = new SendGridClient(apiKey);
              var msg = new SendGridMessage();
      
              var people = new List<Person>
              {
                  new Person {FirstName = "First1", LastName = "Last1", Email = "email1@example.com"},
                  new Person {FirstName = "First2", LastName = "Last2", Email = "email2@example.com"},
                  new Person {FirstName = "First3", LastName = "Last3", Email = "email3@example.com"}
              };
      
              msg.SetFrom(new EmailAddress("test@example.com", "Example User"));
              msg.SetSubject("Test Subject 1");
              msg.AddContent(MimeType.Text, "Hello -firstname- -lastname-");
      
              var tos = new List<EmailAddress>();
      
              var personalizationIndex = 0;
              foreach (var person in people)
              {
                  tos.Add(new EmailAddress(person.Email, person.FirstName));
                  msg.AddSubstitution("-firstname-", person.FirstName, personalizationIndex);
                  msg.AddSubstitution("-lastname-", person.LastName, personalizationIndex);
                  personalizationIndex++;
              }
      
              msg.AddTos(tos);
      
              var response = await client.SendEmailAsync(msg);
          }
      }
      
      internal class Person
      {
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public string Email { get; set; }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-11-10
        • 2021-11-02
        • 2018-07-22
        • 2016-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-06
        • 2020-11-21
        相关资源
        最近更新 更多