In this article, let’s see how to send an email with dynamically generated email content using Razor View (.cshtml) in ASP.NET Core
Create New Solution, Project, References & Add Packages using .NET CLI
Note: I’m using dotnet CLI to create the projects and references. One can use Visual Studio as well.
# Create new solution dotnet new sln -n "EmailService" # Create Razor Class Library dotnet new razorclasslib --support-pages-and-views -n "EmailService.Templates" # Create Web Api dotnet new webapi -n "EmailService.Api" -minimal --no-openapi # Add projects to solution dotnet sln add EmailService.Templates/EmailService.Templates.csproj dotnet sln add EmailService.Api/EmailService.Api.csproj # Add RCL project as reference to Api project cd EmailService.Api dotnet add reference ../EmailService.Templates/EmailService.Templates.csproj # Add following Nuget to Api project dotnet add package FluentEmail.Core dotnet add package FluentEmail.Smtp dotnet add package Razor.Templating.Core
Final Project Structure
Create View Model
I’ve created the view model to be passed to the razor view in this path EmailService\EmailService.Templates\WelcomeEmail\WelcomeEmailModel.cs
namespace EmailService.Templates.WelcomeEmail; public class WelcomeEmailModel { public string Name { get; set; } = null!; public string Email { get; set; } = null!; public string CompanyName { get; set; } = null!; public string SenderName { get; set; } = null!; }
Create Email Template (Razor View .cshtml)
Then, I created the following razor view file in this path EmailService.Templates\WelcomeEmail\WelcomeEmailTemplate.cshtml
Note: This template can be customized as per the needs.
@model EmailService.Templates.WelcomeEmail.WelcomeEmailModel Hello, @Model.Name, Welcome to @Model.CompanyName! Here, It's much easier to do templating using the powerful Razor SDK with the help of Razor.Templating.Core nuget package. Regards, @Model.SenderName
Render Email Template and Send Email
using EmailService.Templates.WelcomeEmail; using FluentEmail.Core; using Razor.Templating.Core; var builder = WebApplication.CreateBuilder(args); builder.Services .AddFluentEmail("fromemail@test.test") .AddSmtpSender("localhost", 25); var app = builder.Build(); app.MapGet("/", async () => { // Create the View Model to be passed to the razor view var model = new WelcomeEmailModel { Name = "John", Email = "john@example.com", CompanyName = "Razor.Templating.Core", SenderName = "Soundar" }; // Generate the HTML content from the Razor View file using Razor.Templating.Core library var body = await RazorTemplateEngine.RenderAsync("/WelcomeEmail/WelcomeEmailTemplate.cshtml", model); // Now, send the generated html content as body using FluentEmail library var email = await Email .From("soundaranbu@gmail.com", model.SenderName) .To(model.Email, model.Name) .Subject($"Welcome to {model.CompanyName}") .Body(body) .SendAsync(); return "Email sent successfully"; }); app.Run();
Conclusion
In this article, we’ve seen how to render the HTML content from razor view using Razor.Templating.Core package and then send the generated HTML content using FluentEmail package.
Code for this article can be found here: RazorTemplating/examples/_RealWorldSamples/EmailService at master · soundaranbu/RazorTemplating (github.com)
Thanks for reading. Happy coding 🙂