Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

4 Commits

Repository files navigation

📧 Azure Function App - SendEmail

This Azure Function App provides a centralized email sending service that can be consumed by various applications using JWT-based authentication. It includes:

  • SendEmail Function: Accepts email payloads and sends emails
  • GetToken Function: Authenticates client apps and issues JWT tokens
  • Built-in validation against a database table for allowed client apps

🚀 Features

  • Centralized email dispatch service
  • JWT-based authentication
  • Request validation with client AppName and API key
  • Database logging of email requests
  • Retry-safe design for resilience
  • Easily consumable by .NET (C#/VB.NET), Python, etc.

📂 Project Structure


🧪 Prerequisites

  • .NET 6 or later
  • Azure CLI or Azure Functions Core Tools
  • Azure Storage Account (for function app)
  • SQL Database (for app authentication and logging)
  • SMTP credentials or integration with SendGrid

⚙️ Setup Instructions

  1. Clone this repository
  2. Set up local settings:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"JwtSecret": "<your-secret>",
"ConnectionStrings:DefaultConnection": "<your-sql-connection-string>"
}
}
  1. Add SQL Tables
CREATETABLEAllowedApps (
Id INTPRIMARY KEY IDENTITY,
AppName NVARCHAR(100) NOT NULL,
ApiKey NVARCHAR(200) NOT NULL,
IsActive BITNOT NULL
);
CREATETABLEEmailLog (
Id INTPRIMARY KEY IDENTITY,
AppName NVARCHAR(100),
MailTo NVARCHAR(200),
MailFrom NVARCHAR(200),
Subject NVARCHAR(500),
Body NVARCHAR(MAX),
Priority NVARCHAR(20),
Status NVARCHAR(20),
RetryCount INT,
CreatedAt DATETIME DEFAULT GETDATE()
);

🔐 Authentication

  1. The client app sends a POST request to /api/gettoken with its AppName and ApiKey
  2. The function returns a JWT if the credentials match an active app in the database
  3. The client must include the token in the Authorization header as Bearer when calling /api/SendEmail

🧪 API Endpoints

POST /api/gettoken

Request Body:
{
"AppName": "ClientApp1",
"ApiKey": "your-api-key"
}
Response:
{
"token": "<jwt-token>"
}

POST /api/SendEmail

Headers:

Authorization: Bearer <jwt-token>

Body:

{
"AppName": "ClientApp1",
"MailTo": "to@example.com",
"MailFrom": "from@example.com",
"MailSubject": "Hello",
"Body": "Test email",
"Priority": "High"
}

🔗 Client Integration Example

✅ Step 1: EmailService.cs

publicclassEmailService{privatereadonlyHttpClient_httpClient;privatereadonlyILogger<EmailService>_logger;privatestring_jwtToken;privatereadonlystring_authUrl="https://your-func-url/api/gettoken";privatereadonlystring_functionUrl="https://your-func-url/api/SendEmail";publicEmailService(HttpClienthttpClient,ILogger<EmailService>logger){_httpClient=httpClient;_logger=logger;}publicasyncTaskInitializeAsync(stringappName,stringapiKey){varauthRequest=new{AppName=appName,ApiKey=apiKey};varresponse=await_httpClient.PostAsJsonAsync(_authUrl,authRequest);response.EnsureSuccessStatusCode();varauthResponse=awaitresponse.Content.ReadFromJsonAsync<AuthResponse>();_jwtToken=authResponse.Token;}publicasyncTaskSendEmailAsync(EmailRequestemailRequest){_httpClient.DefaultRequestHeaders.Authorization=newAuthenticationHeaderValue("Bearer",_jwtToken);varresponse=await_httpClient.PostAsJsonAsync(_functionUrl,emailRequest);response.EnsureSuccessStatusCode();}}

✅ Step 2: Controller Usage

[ApiController][Route("api/[controller]")]publicclassEmailController:ControllerBase{privatereadonlyEmailService_emailService;publicEmailController(EmailServiceemailService){_emailService=emailService;}[HttpPost]publicasyncTask<IActionResult>SendEmail(){await_emailService.InitializeAsync("ClientApp1","your-api-key");await_emailService.SendEmailAsync(newEmailRequest{AppName="ClientApp1",MailTo="to@example.com",MailFrom="from@example.com",MailSubject="Sample Subject",Body="Test Body",Priority="High"});returnOk("Email sent.");}}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages