无法在ASP中使用FluentEmail发送Razor模板.净的核心



我看了Tim Corey关于FluentEmail的视频。我测试了他的代码和一切工作正常,但当我试图实现它在Asp。我无法使用模板发送任何消息。我唯一能做的就是使用body发送消息。
我见过一些人也有同样的问题,但我没有找到任何解决方法。

public async Task SendEmailAsync() {
var sender = new SmtpSender(() => new SmtpClient("smtp.gmail.com") {
UseDefaultCredentials = false,
Port = 587,
Credentials = new NetworkCredential("email", "******"),
EnableSsl = true
});
StringBuilder template = new();
template.AppendLine("Dear @Model.FirstName,");
template.AppendLine("<p>Thanks for purchasing @Model.ProductName. We hope you enjoy it.</p>");
template.AppendLine("- The TimCo Team");
Email.DefaultSender = sender;
Email.DefaultRenderer = new RazorRenderer();
var email = await Email
.From("email")
.To("email", "Sue")
.Subject("Thanks!")
.UsingTemplate(template.ToString(), new { FirstName = "Tim", ProductName = "Bacon-Wrapped Bacon" })
.SendAsync();
}

我还在。csproj文件

中添加了这一行
<PreserveCompilationContext>true</PreserveCompilationContext>

我发现了这个问题。我只需要添加一个引用:

Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

最新更新