Skip to content

Commit

Permalink
Add WebControllerApi example, referenced by docs
Browse files Browse the repository at this point in the history
  • Loading branch information
filipetoscano committed Jan 28, 2025
1 parent d9f3fe5 commit 296d61f
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
| [ConsoleNoDi](https://github.com/resend/resend-dotnet/tree/master/examples/ConsoleNoDi) | n/a | Send email from console app (without dependency injection)
| [RenderLiquid](https://github.com/resend/resend-dotnet/tree/master/examples/RenderLiquid) | 5006 | Render an HTML body using [Fluid](https://github.com/sebastienros/fluid), a [Liquid](https://shopify.github.io/liquid/) template language
| [RenderRazor](https://github.com/resend/resend-dotnet/tree/master/examples/RenderRazor) | 5005 | Render an HTML body using Razor views
| [WebControllerApi](https://github.com/resend/resend-dotnet/tree/master/examples/WebControllerApi) | 5000 | Send email from a controller
| [WebMinimalApi](https://github.com/resend/resend-dotnet/tree/master/examples/WebMinimalApi) | 5001 | Send email from a (minimal) API
| [WebRazor](https://github.com/resend/resend-dotnet/tree/master/examples/WebRazor) | 5002 | Send email from a Razor form

Expand Down
98 changes: 98 additions & 0 deletions examples/WebControllerApi/Controllers/EmailController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Microsoft.AspNetCore.Mvc;
using Resend;
using System.ComponentModel.DataAnnotations;

namespace WebControllerApi.Controllers;

/// <summary />
[ApiController]
public class EmailController : ControllerBase
{
private readonly IResend _resend;
private readonly ILogger<EmailController> _logger;


/// <summary />
public EmailController( IResend resend, ILogger<EmailController> logger )
{
_resend = resend;
_logger = logger;
}


/// <summary>
/// Sends a pre-defined email.
/// </summary>
[HttpGet]
[Route( "email/send" )]
public async Task<string> EmailSendFixed()
{
/*
*
*/
var message = new EmailMessage();
message.From = "you@domain.com";
message.To.Add( "user@gmail.com" );
message.Subject = "Hello from Controller API";
message.TextBody = "Email using Resend .NET SDK";

var resp = await _resend.EmailSendAsync( message );

_logger.LogInformation( "Sent email, with Id = {EmailId}", resp.Content );

return resp.Content.ToString();
}


/// <summary>
/// Sends an email to specified email address.
/// </summary>
[HttpPost]
[Route( "email/send" )]
public async Task<ActionResult<string>> EmailSend( [FromBody] EmailSendRequest request )
{
/*
* Validate
*/
if ( ModelState.IsValid == false )
{
return BadRequest();
}


/*
*
*/
var message = new EmailMessage();
message.From = "you@domain.com";
message.To.Add( request.To );
message.Subject = request.Subject ?? "Hello from Web Controller";
message.TextBody = "Email using Resend .NET SDK";

var resp = await _resend.EmailSendAsync( message );

_logger.LogInformation( "Sent email to {To}, with Id = {EmailId}", request.To, resp.Content );

return resp.Content.ToString();
}


/// <summary>
/// Request payload.
/// </summary>
public class EmailSendRequest
{
/// <summary>
/// Email address of recipient.
/// </summary>
[Required]
[EmailAddress]
public string To { get; set; } = default!;

/// <summary>
/// Subject.
/// </summary>
[StringLength( 100, MinimumLength = 1 )]
public string? Subject { get; set; }
}
}
43 changes: 43 additions & 0 deletions examples/WebControllerApi/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Resend;

namespace WebControllerApi;

/// <summary />
public class Program
{
/// <summary />
public static void Main( string[] args )
{
/*
*
*/
var builder = WebApplication.CreateBuilder( args );

builder.Services.AddAuthorization();
builder.Services.AddControllers();

// Resend
builder.Services.AddOptions();
builder.Services.Configure<ResendClientOptions>( o =>
{
o.ApiToken = Environment.GetEnvironmentVariable( "RESEND_APITOKEN" )!;
} );
builder.Services.AddHttpClient<ResendClient>();
builder.Services.AddTransient<IResend, ResendClient>();


/*
*
*/
var app = builder.Build();

app.UseAuthorization();
app.MapControllers();


/*
*
*/
app.Run();
}
}
31 changes: 31 additions & 0 deletions examples/WebControllerApi/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5000",
"sslPort": 0
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"launchUrl": "",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": false,
"launchUrl": "",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
19 changes: 19 additions & 0 deletions examples/WebControllerApi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.NET SDK: ASP.NET - Controller API
=====================================================================

This example shows how to send emails from an ASP.NET application
using controllers.


How to run
---------------------------------------------------------------------

1. Set the `RESEND_APITOKEN` environment variable to your Resend API.
2. Edit the `From` and `To` in `Controllers/EmailController.cs` as necessary.
3. Run the console app with `dotnet run`.
4. Make a `GET` request to `http://localhost:5000/email/send`

```bash
> set RESEND_APITOKEN=re_8m9gwsVG_6n94KaJkJ42Yj6qSeVvLq9xF
> dotnet run
```
13 changes: 13 additions & 0 deletions examples/WebControllerApi/WebControllerApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Resend\Resend.csproj" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions examples/WebControllerApi/WebControllerApi.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@WebControllerApi_HostAddress = http://localhost:5000

GET {{WebControllerApi_HostAddress}}/email/send
Accept: application/json

###

POST {{WebControllerApi_HostAddress}}/email/send
Content-Type: application/json
Accept: application/json

{
"to": "user@gmail.com"
}

###
8 changes: 8 additions & 0 deletions examples/WebControllerApi/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions examples/WebControllerApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
7 changes: 7 additions & 0 deletions resend-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RenderRazor", "examples\Ren
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RenderLiquid", "examples\RenderLiquid\RenderLiquid.csproj", "{FD4407E3-551D-48F8-9FFB-63E409F4C4BB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebControllerApi", "examples\WebControllerApi\WebControllerApi.csproj", "{64049053-1C1E-43D6-A23B-5A609354934B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -124,6 +126,10 @@ Global
{FD4407E3-551D-48F8-9FFB-63E409F4C4BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD4407E3-551D-48F8-9FFB-63E409F4C4BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD4407E3-551D-48F8-9FFB-63E409F4C4BB}.Release|Any CPU.Build.0 = Release|Any CPU
{64049053-1C1E-43D6-A23B-5A609354934B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{64049053-1C1E-43D6-A23B-5A609354934B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64049053-1C1E-43D6-A23B-5A609354934B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64049053-1C1E-43D6-A23B-5A609354934B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -144,6 +150,7 @@ Global
{0B0219DF-FAFC-4BB6-B642-0F966D075400} = {121B647E-18F4-41CB-AC00-49D5F06D5320}
{C33A6269-8657-4487-8CDD-588210E20B65} = {121B647E-18F4-41CB-AC00-49D5F06D5320}
{FD4407E3-551D-48F8-9FFB-63E409F4C4BB} = {121B647E-18F4-41CB-AC00-49D5F06D5320}
{64049053-1C1E-43D6-A23B-5A609354934B} = {121B647E-18F4-41CB-AC00-49D5F06D5320}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {15EEB6F3-A067-45F5-987C-824BD8FDEAF9}
Expand Down

0 comments on commit 296d61f

Please sign in to comment.