使用 RazorEngine 电子邮件模板在正文中生成 html

Generate html in body with RazorEngine email template

我正在使用 RazorEngine 生成带有模板的电子邮件。

我遇到的问题是无法在电子邮件正文中添加换行符。

var model = new EmailModel
        {
            Destination = "anon@gmail.com",
            Subject = "Some Subject",
            Body = "Hello <br> Break <br> it <br> up"
        };

var service = TemplateManager.RazorService;
var htmlBody = service.RunCompile("EmailTemplate.cshtml", model.GetType(), model);
await EmailService.SendEmail(model.Destination, model.Subject, htmlBody);

我尝试在我的模板中执行以下操作:

@Html.Raw(Model.Body)

但它仍然无法解码 html,有什么想法吗?

您在正文中的 <br> 标签不正确。

<br>替换为<br />

更新:

已在此处找到解决方案:https://github.com/Antaris/RazorEngine/issues/34

这里:RazorEngine: cannot use Html.Raw

It's enough to use @(new RawString("html string here")) or @Raw("html string here") instead of @Html.Raw("html string here").