SendGrid事务模板自定义参数c#

问题描述 投票:1回答:1

我使用asp mvc核心安装了Sendgrid nuget包

我可以成功创建/发送/接收纯文本电子邮件。但是我现在正尝试使用我在sendgrid帐户中创建的跨国模板来传递自定义值以便在模板中进行替换。

当我运行以下内容时,我在响应变量中收到错误的请求错误:

        public async Task<Response> SendTemplateAsync(string to, string toName, string subject, string plainTextContent, string htmlContent, string templateId, IEnumerable<IFormFile> attachments = null)
    {
        var msg = new SendGridMessage
        {
            From = new EmailAddress(_sendGridSettings.EmailFrom, _sendGridSettings.EmailFromName),
            Subject = subject,
            PlainTextContent = plainTextContent,
            HtmlContent = htmlContent,
            TemplateId = templateId
        };
        msg.AddSubstitution("-NAME-", toName.ToString());
        msg.AddSubstitution("-UREF-", "ABC 123".ToString());

        msg.AddTo(new EmailAddress(to, toName));

        var response = await _sendGridClient.SendEmailAsync(msg);

        return response;
    }

我似乎无法访问任何有意义的错误消息,控制台等没有任何内容。当我删除“msg.AddSubstitution”和模板引用时,上面的代码工作。

我读了一些例子,似乎都表明我正在做的是对的。有人也可以解释Substitution和自定义args之间的区别

提前致谢

c# asp.net-core sendgrid
1个回答
0
投票

我收到BadRequest响应,直到我将TemplateId更改为系统分配的神秘ID,而不是我的人类可读版本名称。

© www.soinside.com 2019 - 2024. All rights reserved.