Rendering Emails with Razor Views
Relevant source files
The following files were used as context for generating this wiki page:
The Resend .NET SDK allows you to generate dynamic HTML email content using the Razor view engine. This approach replaces manual string concatenation with structured templates, improving code readability and maintainability.
By using Razor, you separate presentation logic from application logic. You define email layouts in .cshtml files and populate them with strongly-typed models before sending via the IResend client.
Sources: examples/README.md, examples/RenderRazor/README.md:1-5
System Architecture
The rendering system integrates standard ASP.NET Core MVC components with the Resend SDK. The application uses the IRazorViewEngine to locate and compile templates, which are then rendered into strings for the EmailMessage.HtmlBody property.
Core Components
| Component | Description |
|---|---|
IRazorViewEngine | Locates and retrieves the .cshtml view files. |
IResend | The primary client interface for transmitting the rendered email. |
ActionContext | Provides the necessary execution context for the Razor engine. |
ViewContext | Encapsulates the model data and output writer for the rendering process. |
Sources: examples/RenderRazor/Controllers/EmailController.cs:17-40, examples/RenderRazor/Program.cs:20-27
Data Flow
The following diagram illustrates the flow from a request to render an email through to the final transmission via the Resend API.
This process ensures that the Razor engine has all necessary services, such as TempData providers and metadata providers, to execute the view logic.
Sources: examples/RenderRazor/Controllers/EmailController.cs:88-140
Implementation Details
View Engine Configuration
To enable Razor rendering, you must register the required services in the application container. This includes adding controllers and Razor pages to the service collection.
var builder = WebApplication.CreateBuilder( args );
builder.Services.AddControllers();
builder.Services.AddRazorPages();
builder.Services.AddHttpClient<ResendClient>();
builder.Services.AddTransient<IResend, ResendClient>();
Sources: examples/RenderRazor/Program.cs:18-27
Rendering Logic
The RenderEmail<T> method handles the manual invocation of the Razor engine. It performs the following steps:
- View Lookup: Uses
_razor.GetViewwith a specific path (e.g.,~/Views/Email/) to find the template. - Context Creation: Generates a
DefaultHttpContextandActionContext. - Dictionary Setup: Initializes
ViewDataDictionary<T>with the provided model andTempDataDictionary. - Execution: Calls
view.RenderAsyncto write the HTML output into aStringWriter.
Sources: examples/RenderRazor/Controllers/EmailController.cs:88-140
Sequence of Operations
The sequence diagram below shows the interactions between the EmailController, the Razor engine, and the Resend SDK.
Usage Example
To send an email, you instantiate a model, render it to an HTML string, and assign that string to the HtmlBody of an EmailMessage.
var html = await RenderEmail( "Hello", new HelloModel()
{
DisplayName = "John Doe",
Items = { "NodeJs", "Go" },
SenderName = "Resend Team",
} );
var message = new EmailMessage();
message.From = "you@domain.com";
message.To.Add( "user@gmail.com" );
message.Subject = "Hello from Render Razor";
message.HtmlBody = html;
await _resend.EmailSendAsync( message );
Sources: examples/RenderRazor/Controllers/EmailController.cs:52-82, README.md:126-147
Summary
Rendering emails with Razor views provides a robust way to handle complex HTML templates in .NET applications. By leveraging IRazorViewEngine within an API controller, you can generate content-rich emails that remain easy to test and maintain while utilizing the full delivery capabilities of the Resend SDK.
Sources: examples/RenderRazor/README.md:1-5, examples/README.md