【问题标题】:SendGrid dynamic template contains empty data - .NET CoreSendGrid 动态模板包含空数据 - .NET Core
【发布时间】:2020-07-25 10:57:19
【问题描述】:

我正在尝试发送包含特定动态模板和数据的电子邮件。电子邮件已成功发送,但包含空数据。 handle 方法是 Azure Function 的一部分。最近我将 Newtonsoft JSON 更改为 System.Text.Json,这可能会导致一些问题。

电子邮件:

SendGrid 动态模板配置:

C# 代码:

        public async Task Handle(SendEmailCommand command)
        {
            var client = new SendGridClient(emailConfig.SendGridApiKey);
            var msg = new SendGridMessage();

            msg.SetFrom(new EmailAddress(emailConfig.Sender));
            msg.AddTo(new EmailAddress(command.To));
            msg.SetTemplateId(command.SendGridTemplateId);

            if (command.SendGridDynamicTemplateData != null)
            {
                var templateData = new TemplateData();

                command.SendGridDynamicTemplateData.TryGetValue("topic", out var topic);
                command.SendGridDynamicTemplateData.TryGetValue("email", out var email);
                command.SendGridDynamicTemplateData.TryGetValue("name", out var name);
                command.SendGridDynamicTemplateData.TryGetValue("message", out var message);

                templateData.Topic = topic.ToString();
                templateData.Email = email.ToString();
                templateData.Name = name.ToString();
                templateData.Message = message.ToString();

                msg.SetTemplateData(templateData);
            }

            await client.SendEmailAsync(msg);
        }

        private class TemplateData
        {
            [JsonPropertyName("topic")]
            public string Topic { get; set; }

            [JsonPropertyName("email")]
            public string Email { get; set; }

            [JsonPropertyName("name")]
            public string Name { get; set; }

            [JsonPropertyName("message")]
            public string Message { get; set; }
        }

我做错了吗?

【问题讨论】:

    标签: c# asp.net-core .net-core sendgrid sendgrid-templates


    【解决方案1】:

    我注意到了同样的事情。 SendGrid API 似乎无法识别来自System.Text.Json[JsonPropertyName] 属性。我必须使用 Newtonsoft.Json 中的 [JsonProperty] 属性才能让参数出现在电子邮件中。

    【讨论】:

    • SendGrid API 似乎无法与 System.Text.Json 一起正常工作,System.Text.Json 目前是 .NET Core 的推荐序列化程序(比 Newtonsoft.Json 更快)
    • 更糟糕的是,似乎 api 不会让你直接给他们 JSON
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多