SendEmail.Core
2.0.1
dotnet add package SendEmail.Core --version 2.0.1
NuGet\Install-Package SendEmail.Core -Version 2.0.1
<PackageReference Include="SendEmail.Core" Version="2.0.1" />
<PackageVersion Include="SendEmail.Core" Version="2.0.1" />
<PackageReference Include="SendEmail.Core" />
paket add SendEmail.Core --version 2.0.1
#r "nuget: SendEmail.Core, 2.0.1"
#:package SendEmail.Core@2.0.1
#addin nuget:?package=SendEmail.Core&version=2.0.1
#tool nuget:?package=SendEmail.Core&version=2.0.1
SendEmail v2.0
Biblioteca modular para enviar correos electrónicos en .NET 9 utilizando MailKit con autenticación OAuth2.
🚀 Características v2.0
- ✅ OAuth2 para Gmail con auto-refresh de tokens
- ✅ CLI incluido para obtener tokens OAuth2 fácilmente
- ✅ Variables de entorno para configuración segura
- ✅ Inmutable value objects y builder pattern
- ✅ Dependency Injection integrado
- ✅ .NET 9 compatible
Instalación
NuGet
dotnet add package SendEmail.Core
dotnet add package SendEmail.MailKit
Proyectos en la solución
- SendEmail.Core: Abstracciones y modelos
- SendEmail.MailKit: Implementación SMTP con OAuth2
- SendEmail.OAuth2.Cli: Herramienta CLI para obtener tokens
- SendEmail.Console: Proyecto de ejemplo
🔐 Configuración OAuth2 (Nuevo en v2.0)
Paso 1: Obtener Credenciales
- Sigue la guía completa: docs/oauth2-setup.md
- Crea proyecto en Google Cloud Console
- Habilita Gmail API
- Obtén Client ID y Client Secret
Paso 2: Obtener Refresh Token
Usa la herramienta CLI incluida:
dotnet run --project SendEmail.OAuth2.Cli
Paso 3: Configurar
Opción A: Variables de Entorno (Recomendado)
# Windows
$env:SENDMAIL_OAUTH_CLIENT_ID="tu-client-id.apps.googleusercontent.com"
$env:SENDMAIL_OAUTH_CLIENT_SECRET="tu-client-secret"
$env:SENDMAIL_OAUTH_REFRESH_TOKEN="tu-refresh-token"
# Linux/macOS
export SENDMAIL_OAUTH_CLIENT_ID="tu-client-id.apps.googleusercontent.com"
export SENDMAIL_OAUTH_CLIENT_SECRET="tu-client-secret"
export SENDMAIL_OAUTH_REFRESH_TOKEN="tu-refresh-token"
Opción B: appsettings.json
{
"Smtp": {
"Host": "smtp.gmail.com",
"Port": 587,
"UseStartTls": true,
"OAuth2": {
"ClientId": "tu-client-id.apps.googleusercontent.com",
"ClientSecret": "tu-client-secret",
"RefreshToken": "tu-refresh-token"
},
"DefaultFrom": {
"Address": "tu-email@gmail.com",
"DisplayName": "Tu Nombre"
}
}
}
Uso Rápido
Configuración con DI
var builder = Host.CreateApplicationBuilder(args);
// Lee automáticamente de appsettings.json y variables de entorno
builder.Services.AddMailKitEmailClient(builder.Configuration, "Smtp");
var host = builder.Build();
Envío de Email
var emailClient = host.Services.GetRequiredService<IEmailClient>();
var message = EmailMessageBuilder.Create()
.To("destinatario@example.com", "Nombre Destinatario")
.WithSubject("Hola desde SendEmail v2.0")
.WithHtmlBody("<h1>Hola!</h1><p>Email con OAuth2</p>")
.WithTextBody("Hola! Email con OAuth2")
.Build();
var result = await emailClient.SendAsync(message);
if (result.IsSuccess)
{
Console.WriteLine("✓ Email enviado exitosamente!");
}
📚 Documentación
- OAuth2 Setup Guide - Configuración paso a paso de Google Cloud
- Migration Guide v2.0 - Migración desde v1.x
- Console Example - Proyecto de ejemplo completo
🔄 Migración desde v1.x
BREAKING CHANGE: v2.0 elimina soporte para Username/Password.
Antes (v1.x):
options.Username = "email@gmail.com"; // ❌ Ya no funciona
options.Password = "app-password"; // ❌ Ya no funciona
Ahora (v2.0):
options.OAuth2 = new OAuth2Options // ✅ Requerido
{
ClientId = "...",
ClientSecret = "...",
RefreshToken = "..."
};
Ver guía completa: docs/migration-v2.md
🛠️ Desarrollo
# Build
dotnet build
# Tests
dotnet test
# Ejecutar ejemplo
dotnet run --project SendEmail.Console
# Ejecutar CLI para tokens
dotnet run --project SendEmail.OAuth2.Cli
Plantillas (Template Rendering)
Implemente IEmailTemplateRenderer para renderizado dinámico:
public interface IEmailTemplateRenderer
{
Task<EmailContent> RenderTemplateAsync<TModel>(string templateName, TModel model);
}
📦 Paquetes NuGet
Licencia
MIT License - Ver LICENSE para detalles.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- System.Collections.Immutable (>= 9.0.9)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on SendEmail.Core:
| Package | Downloads |
|---|---|
|
SendEmail.MailKit
MailKit-based SMTP client with OAuth2 authentication for Gmail. Includes automatic token refresh and DI support. |
GitHub repositories
This package is not used by any popular GitHub repositories.
v2.0.1: Fix OAuth2 authentication (use email instead of ClientId). Improve security and documentation.