Fix: Configuración SMTP
All checks were successful
Optimized Build and Deploy / remote-build-and-deploy (push) Successful in 6m46s
All checks were successful
Optimized Build and Deploy / remote-build-and-deploy (push) Successful in 6m46s
- No se toma la configuración SMTP del .env por falla de lecturas. - Se incluyen las configuraciones en appsettings.json
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
# ================================================
|
||||
# VARIABLES DE ENTORNO PARA LA CONFIGURACIÓN DE CORREO
|
||||
# ================================================
|
||||
# El separador de doble guion bajo (__) se usa para mapear la jerarquía del JSON.
|
||||
# MailSettings:SmtpHost se convierte en MailSettings__SmtpHost
|
||||
|
||||
MailSettings__SmtpHost="192.168.5.201"
|
||||
MailSettings__SmtpPort=587
|
||||
MailSettings__SenderName="Club - Diario El Día"
|
||||
MailSettings__SenderEmail="alertas@eldia.com"
|
||||
MailSettings__SmtpUser="alertas@eldia.com"
|
||||
MailSettings__SmtpPass="@Alertas713550@"
|
||||
@@ -9,7 +9,6 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="9.0.0" />
|
||||
<PackageReference Include="Dapper" Version="2.1.66" />
|
||||
<PackageReference Include="DotNetEnv" Version="3.1.1" />
|
||||
<PackageReference Include="MailKit" Version="4.13.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.4" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
|
||||
|
||||
@@ -24,10 +24,6 @@ using GestionIntegral.Api.Models.Comunicaciones;
|
||||
using GestionIntegral.Api.Services.Comunicaciones;
|
||||
using GestionIntegral.Api.Data.Repositories.Comunicaciones;
|
||||
|
||||
// Carga las variables de entorno desde el archivo .env al inicio de la aplicación.
|
||||
// Debe ser la primera línea para que la configuración esté disponible para el 'builder'.
|
||||
DotNetEnv.Env.Load();
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// --- Registros de Servicios ---
|
||||
|
||||
@@ -4,6 +4,8 @@ using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MimeKit;
|
||||
using System.Net.Security;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace GestionIntegral.Api.Services.Comunicaciones
|
||||
{
|
||||
@@ -88,6 +90,30 @@ namespace GestionIntegral.Api.Services.Comunicaciones
|
||||
using var smtp = new SmtpClient();
|
||||
try
|
||||
{
|
||||
// Se añade una política de validación de certificado personalizada.
|
||||
// Esto es necesario para entornos de desarrollo o redes internas donde
|
||||
// el nombre del host al que nos conectamos (ej. una IP) no coincide
|
||||
// con el nombre en el certificado SSL (ej. mail.eldia.com).
|
||||
smtp.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
|
||||
{
|
||||
// Si no hay errores, el certificado es válido.
|
||||
if (sslPolicyErrors == SslPolicyErrors.None)
|
||||
return true;
|
||||
|
||||
// Si el único error es que el nombre no coincide (RemoteCertificateNameMismatch)
|
||||
// Y el certificado es el que esperamos (emitido para "mail.eldia.com"),
|
||||
// entonces lo aceptamos como válido.
|
||||
if (sslPolicyErrors.HasFlag(SslPolicyErrors.RemoteCertificateNameMismatch) && certificate != null && certificate.Subject.Contains("CN=mail.eldia.com"))
|
||||
{
|
||||
_logger.LogWarning("Se aceptó un certificado SSL con 'Name Mismatch' para el host de confianza 'mail.eldia.com'.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Para cualquier otro error, rechazamos el certificado.
|
||||
_logger.LogError("Error de validación de certificado SSL: {Errors}", sslPolicyErrors);
|
||||
return false;
|
||||
};
|
||||
|
||||
await smtp.ConnectAsync(_mailSettings.SmtpHost, _mailSettings.SmtpPort, SecureSocketOptions.StartTls);
|
||||
await smtp.AuthenticateAsync(_mailSettings.SmtpUser, _mailSettings.SmtpPass);
|
||||
await smtp.SendAsync(emailMessage);
|
||||
@@ -95,20 +121,6 @@ namespace GestionIntegral.Api.Services.Comunicaciones
|
||||
log.Estado = "Enviado";
|
||||
_logger.LogInformation("Email enviado exitosamente a {Destinatario}. Asunto: {Asunto}", destinatario, emailMessage.Subject);
|
||||
}
|
||||
catch (SmtpCommandException scEx)
|
||||
{
|
||||
_logger.LogError(scEx, "Error de comando SMTP al enviar a {Destinatario}. StatusCode: {StatusCode}", destinatario, scEx.StatusCode);
|
||||
log.Estado = "Fallido";
|
||||
log.Error = $"Error del servidor: ({scEx.StatusCode}) {scEx.Message}";
|
||||
throw;
|
||||
}
|
||||
catch (AuthenticationException authEx)
|
||||
{
|
||||
_logger.LogError(authEx, "Error de autenticación con el servidor SMTP.");
|
||||
log.Estado = "Fallido";
|
||||
log.Error = "Error de autenticación. Revise las credenciales de correo.";
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error general al enviar email a {Destinatario}. Asunto: {Asunto}", destinatario, emailMessage.Subject);
|
||||
|
||||
@@ -16,11 +16,11 @@
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"MailSettings": {
|
||||
"SmtpHost": "",
|
||||
"SmtpPort": 0,
|
||||
"SenderName": "",
|
||||
"SenderEmail": "",
|
||||
"SmtpUser": "",
|
||||
"SmtpPass": ""
|
||||
"SmtpHost": "192.168.5.201",
|
||||
"SmtpPort": 587,
|
||||
"SenderName": "Club - Diario El Día",
|
||||
"SenderEmail": "alertas@eldia.com",
|
||||
"SmtpUser": "alertas@eldia.com",
|
||||
"SmtpPass": "@Alertas713550@"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user