dotnet-email-service-sendgrid
Implements email service abstraction with SendGrid provider. Includes template support, localization, async sending, and domain event integration for transactional emails.
git clone --depth 1 https://github.com/ronnythedev/dotnet-clean-architecture-skills /tmp/dotnet-email-service-sendgrid && cp -r /tmp/dotnet-email-service-sendgrid/skills/16.1-dotnet-email-service-sendgrid ~/.claude/skills/dotnet-email-service-sendgridSKILL.md
# Email Service Integration
## Overview
Abstracted email service with template support:
- **Interface in Application** - `IEmailService`
- **SendGrid implementation** - Production-ready provider
- **Template support** - Dynamic content with placeholders
- **Localization** - Multiple language templates
- **Domain event integration** - Send on user actions
## Quick Reference
| Component | Purpose |
|-----------|---------|
| `IEmailService` | Email abstraction interface |
| `SendGridEmailService` | SendGrid implementation |
| `EmailOptions` | Configuration settings |
| `EmailTemplate` | Template definitions |
---
## Template: Email Service Interface
```csharp
// src/{name}.application/Abstractions/Email/IEmailService.cs
namespace {name}.application.abstractions.email;
public interface IEmailService
{
Task SendAsync(
string to,
string subject,
string htmlBody,
CancellationToken cancellationToken = default);
Task SendTemplateAsync(
string to,
string templateId,
object templateData,
CancellationToken cancellationToken = default);
Task SendWelcomeEmailAsync(
string to,
string userName,
string language = "en",
CancellationToken cancellationToken = default);
Task SendPasswordResetEmailAsync(
string to,
string resetCode,
string language = "en",
CancellationToken cancellationToken = default);
Task SendAssessmentReadyEmailAsync(
string to,
string userName,
string assessmentName,
string language = "en",
CancellationToken cancellationToken = default);
}
```
---
## Template: Email Options
```csharp
// src/{name}.infrastructure/Email/EmailOptions.cs
namespace {name}.infrastructure.email;
public sealed class EmailOptions
{
public const string SectionName = "Email";
public string ApiKey { get; init; } = string.Empty;
public string FromEmail { get; init; } = string.Empty;
public string FromName { get; init; } = string.Empty;
public bool EnableSending { get; init; } = true;
// SendGrid template IDs
public EmailTemplateIds Templates { get; init; } = new();
}
public sealed class EmailTemplateIds
{
public string WelcomeEn { get; init; } = string.Empty;
public string WelcomeEs { get; init; } = string.Empty;
public string PasswordResetEn { get; init; } = string.Empty;
public string PasswordResetEs { get; init; } = string.Empty;
public string AssessmentReadyEn { get; init; } = string.Empty;
public string AssessmentReadyEs { get; init; } = string.Empty;
}
```
---
## Template: SendGrid Implementation
```csharp
// src/{name}.infrastructure/Email/SendGridEmailService.cs
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SendGrid;
using SendGrid.Helpers.Mail;
using {name}.application.abstractions.email;
namespace {name}.infrastructure.email;
internal sealed class SendGridEmailService : IEmailService
{
private readonly ISendGridClient _client;
private readonly EmailOptions _options;
private readonly ILogger<SendGridEmailService> _logger;
public SendGridEmailService(
IOptions<EmailOptions> options,
ILogger<SendGridEmailService> logger)
{
_options = options.Value;
_client = new SendGridClient(_options.ApiKey);
_logger = logger;
}
public async Task SendAsync(
string to,
string subject,
string htmlBody,
CancellationToken cancellationToken = default)
{
if (!_options.EnableSending)
{
_logger.LogInformation(
"Email sending disabled. Would send to {To}: {Subject}",
to, subject);
return;
}
var message = new SendGridMessage
{
From = new EmailAddress(_options.FromEmail, _options.FromName),
Subject = subject,
HtmlContent = htmlBody
};
message.AddTo(new EmailAddress(to));
var response = await _client.SendEmailAsync(message, cancellationToken);
if (!response.IsSuccessStatusCode)
{
var body = await response.Body.ReadAsStringAsync(cancellationToken);
_logger.LogError(
"Failed to send email to {To}. Status: {Status}, Body: {Body}",
to, response.StatusCode, body);
throw new EmailSendException($"Failed to send email: {response.StatusCode}");
}
_logger.LogInformation("Email sent to {To}: {Subject}", to, subject);
}
public async Task SendTemplateAsync(
string to,
string templateId,
object templateData,
CancellationToken cancellationToken = default)
{
if (!_options.EnableSending)
{
_logger.LogInformation(
"Email sending disabled. Would send template {TemplateId} to {To}",
templateId, to);
return;
}
var message = new SendGridMessage
{
From = new EmailAddress(_options.FromEmail, _options.FromName),
TemplateId = templateId
};
message.AddTo(new EmailAddress(to));
message.SetTemplateData(templateData);
var response = await _client.SendEmailAsync(message, cancellationToken);
if (!response.IsSuccessStatusCode)
{
_logger.LogError(
"Failed to send template email to {To}. Template: {TemplateId}",
to, templateId);
throw new EmailSendException($"Failed to send email: {response.StatusCode}");
}
_logger.LogInformation(
"Template email sent to {To}. Template: {TemplateId}",
to, templateId);
}
public async Task SendWelcomeEmailAsync(
string to,
string userName,
string language = "en",
CancellationToken cancellationToken = default)
{
var templateId = lScaffolds a complete .NET solution following Clean Architecture principles with proper layer separation (API, Application, Domain, Infrastructure). Creates project structure, dependency injection setup, and cross-cutting concerns configuration.
Generates CQRS Commands with Handlers, Validators, and Request DTOs following Clean Architecture patterns. Commands represent actions that modify state and return Result types for proper error handling.
Generates CQRS Queries with Handlers and Response DTOs for read operations. Uses Dapper for optimized read queries, bypassing the domain model for better performance.
Generates Domain Entities following DDD principles with factory methods, private setters, domain events, and proper encapsulation. Supports aggregate roots, child entities, and value objects.
Generates Repository interfaces and implementations following the Repository pattern. Provides data access abstraction for aggregate roots with EF Core implementations.
Generates Entity Framework Core configurations using Fluent API. Maps domain entities to database tables with proper relationships, constraints, and conventions.
Generates RESTful API Controllers with proper routing, versioning, authorization, and MediatR integration. Follows REST conventions and Clean Architecture patterns.
Generates Minimal API endpoints following Microsoft's recommended approach. Creates fast, testable HTTP APIs with minimal code using MapGet/MapPost/MapPut/MapDelete. Preferred over controller-based APIs for new projects.