Applica.Emailing
0.1.2
dotnet add package Applica.Emailing --version 0.1.2
NuGet\Install-Package Applica.Emailing -Version 0.1.2
<PackageReference Include="Applica.Emailing" Version="0.1.2" />
<PackageVersion Include="Applica.Emailing" Version="0.1.2" />
<PackageReference Include="Applica.Emailing" />
paket add Applica.Emailing --version 0.1.2
#r "nuget: Applica.Emailing, 0.1.2"
#:package Applica.Emailing@0.1.2
#addin nuget:?package=Applica.Emailing&version=0.1.2
#tool nuget:?package=Applica.Emailing&version=0.1.2
Applica.Emailing
Librería de C# (.NET 8) para envío de correos vía SMTP con MailKit.
- Diseño: SOLID, namespaces con file-scope, constructores primarios y ⇐ 3 argumentos por método.
- DI: extensiones para IServiceCollection.
- Seguridad: no expone secretos en código; use variables de entorno o gestores de secretos.
- Autenticación recomendada (por defecto): OAuth 2.0 (XOAUTH2) para Gmail con scope https://mail.google.com/.
Instalación (proyecto consumidor)
Paquete NuGet publicado: https://www.nuget.org/packages/Applica.Emailing Instalar con dotnet CLI:
dotnet add package Applica.Emailing
o agregar en tu .csproj (permitir parches dentro de la serie 0.1.x):
<ItemGroup>
<PackageReference Include="Applica.Emailing" Version="0.1.*" />
</ItemGroup>
- Referencie el paquete/proyecto
Applica.Emailing
. - Configure SMTP por código o por configuración.
Opción recomendada: OAuth2 (Gmail XOAUTH2)
Ejemplo de appsettings.Development.json (credenciales en variables de entorno/secret manager; no en git):
{ "GmailOAuth": { "ClientId": "{{GOOGLE_CLIENT_ID}}", "ClientSecret": "{{GOOGLE_CLIENT_SECRET}}", "RefreshToken": "{{GOOGLE_REFRESH_TOKEN}}" }, "Emailing": { "Host": "smtp.gmail.com", "Port": 587, "UseSsl": true, "Username": "tu-cuenta@gmail.com", "AuthMode": "OAuth2", "DefaultFrom": { "Address": "tu-cuenta@gmail.com", "DisplayName": "Notificaciones" } } }
Registro en DI con AccessTokenFactory (requiere el paquete Google.Apis.Auth en TU proyecto consumidor):
services.AddEmailing(opts ⇒ { var gmail = configuration.GetSection("GmailOAuth"); var flow = new Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow( new Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new Google.Apis.Auth.OAuth2.ClientSecrets { ClientId = gmail["ClientId"], ClientSecret = gmail["ClientSecret"] } });
opts.Host = "smtp.gmail.com";
opts.Port = 587;
opts.UseSsl = true; // STARTTLS
opts.AuthMode = Applica.Emailing.Implementations.Smtp.SmtpAuthMode.OAuth2;
opts.Username = configuration["Emailing:Username"]; // debe ser tu-cuenta@gmail.com
opts.DefaultFrom = new Applica.Emailing.Models.EmailAddress(configuration["Emailing:Username"], "Notificaciones");
opts.AccessTokenFactory = async ct =>
{
var token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse { RefreshToken = gmail["RefreshToken"] };
var cred = new Google.Apis.Auth.OAuth2.UserCredential(flow, "user", token);
return await cred.GetAccessTokenForRequestAsync("https://mail.google.com/", ct);
};
});
Ver guía paso a paso en
docs/HELP_GMAIL.md
.
Alternativa: App Password (Gmail)
{ "Emailing": { "Host": "smtp.gmail.com", "Port": 587, "UseSsl": true, "Username": "tu-cuenta@gmail.com", "Password": "{{GMAIL_APP_PASSWORD}}", "DefaultFrom": { "Address": "tu-cuenta@gmail.com", "DisplayName": "Notificaciones" } } }
services.AddEmailing(configuration, "Emailing");
Enviar correo
var sender = serviceProvider.GetRequiredService<Applica.Emailing.Abstractions.IEmailSender>(); var msg = new Applica.Emailing.Models.EmailMessage(new Applica.Emailing.Models.EmailMessageData { To = new[]{ new Applica.Emailing.Models.EmailAddress("destinatario@example.com", "Destinatario") }, Subject = "Hola", BodyText = "Hola mundo (texto)", BodyHtml = "<p>Hola mundo</p>" }); await sender.SendAsync(msg);
Notas
- Scope para XOAUTH2 SMTP:
https://mail.google.com/
. - Cumple la regla de ⇐ 3 parámetros por método.
- Sin
git add
/commit
automáticos; sólogit init
. - .NET 8; si desea NET9, cambie el target framework en el csproj.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. |
-
net8.0
- MailKit (>= 4.13.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.9)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.9)
- Microsoft.Extensions.Options (>= 9.0.9)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.