I'm struggling to make email work while working with embedded resource for _Layout.cshtml
in FluentEmail
.
This is the exception error I'm getting:
Project can not find template with key _Layout.cshtml
This is my setup so far:
Inside the ConfigureServices in Program.cs
, I've added the RazorRenderer
as:
//Set email service using FluentEmail
services.AddFluentEmail("appname@domain.com")
.AddRazorRenderer(typeof(Program))
.AddSmtpSender("smtp.somesmtp.com", 25)
.AddSmtpSender(new System.Net.Mail.SmtpClient() { });
Inside my NotificationService.cs
, I always fall into that exception block:
private async Task SendEmailAsync<TModel>(string subject, TModel model)
{
try
{
using (var scope = _serviceProvider.CreateScope())
{
var email = await scope.ServiceProvider.GetRequiredService<IFluentEmail>()
.To(string.Join(";", _emailRecipients))
.Subject(subject)
.UsingTemplateFromEmbedded("AppName.Views.Emails.SomeReport.cshtml", model, GetType().Assembly)
.SendAsync();
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to send email. Check exception for more information.");
}
}
SomeReport.cshtml
is inside Views\Emails\SomeReport.cshtml
which looks like this:
@using System;
@using RazorLight;
@using System.Collections.Generic;
@using AppName.Models;
@inherits TemplatePage<IEnumerable<SomeReport>>
@{
@* For working with Embedded Resource Views *@
Layout = "_Layout.cshtml";
}
@* Work with the Model here... *@
_Layout.cshtml
is inside Views\Shared\_Layout.cshtml
which looks like this:
@using RazorLight;
@* Some common layout styles here *@
@RenderBody()
Both the SomeReport.cshtml
and _Layout.cshtml
are Embedded resources:

My references has RazorLight
through FluentEmail.Razor
package.

If it helps, this is a .NET 5 Worker Service
project and I've also added PreserveCompilationContext
and PreserveCompilationReferences
in the .csproj
file:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<PreserveCompilationReferences>true</PreserveCompilationReferences>
</PropertyGroup>
I've looked at everywhere and still haven't found a solution to this. Something this simple has been such a struggle to make work. Please help.
Thanks!