Reputation: 99
I am having trouble adding an additional substitution for sending emails with Sendgrid C#. https://github.com/sendgrid/sendgrid-csharp/ For example how can I add one more substitution for -name- ? How can I do this? Thank you for your help.
Here is my code that is working fine for one substitution (email).
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
);
Upvotes: 1
Views: 4904
Reputation: 99
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 = "[email protected]"},
new Person {FirstName = "First2", LastName = "Last2", Email = "[email protected]"},
new Person {FirstName = "First3", LastName = "Last3", Email = "[email protected]"}
};
msg.SetFrom(new EmailAddress("[email protected]", "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; }
}
}
Upvotes: 0
Reputation: 2503
An example of dynamic transactional email templates of sendgrid C# API v3.0.
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage();
msg.SetFrom(new EmailAddress("[email protected]", "Example User"));
msg.AddTo(new EmailAddress("[email protected]", "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; }
}
Template body:
<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>
More info: https://github.com/sendgrid/sendgrid-csharp/blob/master/USE_CASES.md#transactional-templates
Upvotes: 3