'SendGrid Dynamic Email Template won't pass data from .NET
I'm trying to use SendGrid's API to send Data to a Dynamic Template, and send that email to a customer. However, the email sends, but the data is never passed. Can anyone tell me what's wrong in my code? I don't see where my error is. I'm using C# .NET.
Here is my C# code
var Email_Client = new SendGridClient(apikey);
EmailAddress from = new EmailAddress(Me);
EmailAddress to = new EmailAddress(Customer);
var template = "x-xxxxxxxxxxxxxx";
var home = new house
{
Name = "test",
Price = price1
};
var email = MailHelper.CreateSingleTemplateEmail(from, to, template, home);
var emailResponse = await Email_Client.SendEmailAsync(email);
The odd thing is if I change my code to this:
var json = Newtonsoft.Json.JsonConvert.SerializeObject(new
{
homes = home
});
var email = MailHelper.CreateSingleTemplateEmail(from, to, template, json);
var emailResponse = await Email_Client.SendEmailAsync(email);
The email never gets sent.
The Email Template has HTML like this
<tbody>
{{#each homes}}
<tr>
<td style="width:200px;max-width:33.333%;"><img class="max-width" border="0" style="display:block;color:#000000;text-decoration:none;font-family:Helvetica, arial, sans-serif;font-size:16px;max-width:100% !important;width:100%;height:auto !important;" src="{{Img}}" alt="" width="200"></td>
<td style="width:200px;max-width:33.333%;"><div style="text-align: center;">{{Name}}</div>
</td>
<td style="width:200px;max-width:33.333%;"><div style="text-align: center;">{{Price}}</div></td>
</tr>
{{/each}}
</tbody>
Any guidance you can give will be very helpful. I've been wrapping my head around this for a while.
Solution 1:[1]
You don't need to serialize your dynamic template data to JSON yourself. However, you do have to make sure the data that you pass in from .NET matches how you're using it in the template.
In the template you're using homes as an array by iterating over it.
So you have to pass in an object with a property homes containing an array.
The array has to have objects with properties Name and Price.
Update your code like this to have template data that matches your template:
var dynamicTemplateData = new
{
homes = new object[]
{
new
{
Name = "test",
Price = price1
}
}
};
var email = MailHelper.CreateSingleTemplateEmail(from, to, template, dynamicTemplateData);
Other tips:
- You used the
homesvariables all lowercased, while usingNameandPricewith an uppercase first letter. Keep your casing consistent to avoid confusion. If you change this, make sure your casing matches between template variables and template data from .NET. - I took the liberty to create anonymous objects instead of using your
houseclass since you didn't share the class. If you're using a class following C# naming conventions (PascalCasing), but want to use other casing for the variables in your email template, you can mark your properties using theJsonPropertyattribute fromNewtonsoft.Json. See theExampleTemplateDatain the following sample. - Make sure your
<tbody>tag is nested inside a<table>tag.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Swimburger |
