-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WebControllerApi example, referenced by docs
- Loading branch information
1 parent
d9f3fe5
commit 296d61f
Showing
10 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
|
||
### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters