添加额外的替代Sendgrid C#

时间:2018-02-23 01:15:36

标签: c# sendgrid

我在使用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
                                                          );

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

示例: 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; }
}
}