简体   繁体   English

SendGrid 动态 Email 模板不会从 .NET 传递数据

[英]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.我正在尝试使用 SendGrid 的 API 将数据发送到动态模板,并将 email 发送给客户。 However, the email sends, but the data is never passed.但是,email 发送,但数据从未传递。 Can anyone tell me what's wrong in my code?谁能告诉我我的代码有什么问题? I don't see where my error is.我没有看到我的错误在哪里。 I'm using C# .NET.我正在使用 C# .NET。

Here is my C# code这是我的 C# 代码

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. email 永远不会被发送。

The Email Template has HTML like this Email 模板有这样的 HTML

<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.一段时间以来,我一直在思考这个问题。

You don't need to serialize your dynamic template data to JSON yourself.您不需要自己将动态模板数据序列化为 JSON。 However, you do have to make sure the data that you pass in from .NET matches how you're using it in the template.但是,您必须确保从 .NET 传入的数据与您在模板中的使用方式相匹配。

In the template you're using homes as an array by iterating over it.在模板中,您通过迭代将homes用作数组。 So you have to pass in an object with a property homes containing an array.所以你必须传入一个 object 和一个包含数组的homes The array has to have objects with properties Name and Price .该数组必须具有属性NamePrice的对象。

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 homes variables all lowercased, while using Name and Price with an uppercase first letter.您使用全小写的homes变量,同时使用带有大写首字母的NamePrice Keep your casing consistent to avoid confusion.保持外壳一致以避免混淆。 If you change this, make sure your casing matches between template variables and template data from .NET.如果更改此设置,请确保模板变量和来自 .NET 的模板数据之间的大小写匹配。
  • I took the liberty to create anonymous objects instead of using your house class since you didn't share the class.我冒昧地创建了匿名对象,而不是使用您的house class,因为您没有共享 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 the JsonProperty attribute from Newtonsoft.Json . 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 the JsonProperty attribute from Newtonsoft.Json . See the ExampleTemplateData in the following sample .请参阅以下示例中的ExampleTemplateData
  • Make sure your <tbody> tag is nested inside a <table> tag.确保您的<tbody>标记嵌套在<table>标记内。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM