.NET library for the Resend API.
> dotnet add package Resend
Send email with:
- ASP.NET - Minimal API - Send email from an API
- ASP.NET - Razor - Send email from a Razor web application
- Console app - Send email from console app (without dependency injection)
- Async - Hangfire - Send email as a background job using Hangfire
- Async - Temporal - Send email in durable workflow using Temporal
- Render - Razor - Render an HTML body using Razor views
- Render - Liquid - Render an HTML body using Fluid, a Liquid template language
First, you need to get an API key, which is available in the Resend Dashboard.
In the startup of your application, configure the DI container as follows:
using Resend;
builder.Services.AddOptions();
builder.Services.AddHttpClient<ResendClient>();
builder.Services.Configure<ResendClientOptions>( o =>
{
o.ApiToken = Environment.GetEnvironmentVariable( "RESEND_APITOKEN" )!;
} );
builder.Services.AddTransient<IResend, ResendClient>()
You can then use the injected IResend
instance to send emails.
Send your first email:
using Resend;
public class FeatureImplementation
{
private readonly IResend _resend;
public FeatureImplementation( IResend resend )
{
_resend = resend;
}
public Task Execute()
{
var message = new EmailMessage();
message.From = "[email protected]";
message.To.Add( "[email protected]" );
message.Subject = "hello world";
message.TextBody = "it works!";
await _resend.EmailSendAsync( message );
}
}
Send an email custom HTML content:
using Resend;
public class FeatureImplementation
{
private readonly IResend _resend;
public FeatureImplementation( IResend resend )
{
_resend = resend;
}
public Task Execute()
{
var message = new EmailMessage();
message.From = "[email protected]";
message.To.Add( "[email protected]" );
message.Subject = "hello world";
message.HtmlBody = "<strong>it works!</strong>";
await _resend.EmailSendAsync( message );
}
}
MIT License