SendEmail.Core 2.0.1

dotnet add package SendEmail.Core --version 2.0.1
                    
NuGet\Install-Package SendEmail.Core -Version 2.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SendEmail.Core" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SendEmail.Core" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="SendEmail.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SendEmail.Core --version 2.0.1
                    
#r "nuget: SendEmail.Core, 2.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SendEmail.Core@2.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SendEmail.Core&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=SendEmail.Core&version=2.0.1
                    
Install as a Cake Tool

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

  1. SendEmail.Core: Abstracciones y modelos
  2. SendEmail.MailKit: Implementación SMTP con OAuth2
  3. SendEmail.OAuth2.Cli: Herramienta CLI para obtener tokens
  4. SendEmail.Console: Proyecto de ejemplo

🔐 Configuración OAuth2 (Nuevo en v2.0)

Paso 1: Obtener Credenciales

  1. Sigue la guía completa: docs/oauth2-setup.md
  2. Crea proyecto en Google Cloud Console
  3. Habilita Gmail API
  4. 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

🔄 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

  • SendEmail.Core Version
  • SendEmail.MailKit Version

Licencia

MIT License - Ver LICENSE para detalles.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
2.0.1 246 10/8/2025
2.0.0 241 10/7/2025
1.0.0 160 9/26/2025

v2.0.1: Fix OAuth2 authentication (use email instead of ClientId). Improve security and documentation.