diff --git a/Backend/GestionIntegral.Api/.env b/Backend/GestionIntegral.Api/.env deleted file mode 100644 index 7c1ccc0..0000000 --- a/Backend/GestionIntegral.Api/.env +++ /dev/null @@ -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@" \ No newline at end of file diff --git a/Backend/GestionIntegral.Api/GestionIntegral.Api.csproj b/Backend/GestionIntegral.Api/GestionIntegral.Api.csproj index 4042988..808f888 100644 --- a/Backend/GestionIntegral.Api/GestionIntegral.Api.csproj +++ b/Backend/GestionIntegral.Api/GestionIntegral.Api.csproj @@ -9,7 +9,6 @@ - diff --git a/Backend/GestionIntegral.Api/Program.cs b/Backend/GestionIntegral.Api/Program.cs index 0ec1aaa..064ad81 100644 --- a/Backend/GestionIntegral.Api/Program.cs +++ b/Backend/GestionIntegral.Api/Program.cs @@ -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 --- diff --git a/Backend/GestionIntegral.Api/Services/Comunicaciones/EmailService.cs b/Backend/GestionIntegral.Api/Services/Comunicaciones/EmailService.cs index 764692d..1e8c64e 100644 --- a/Backend/GestionIntegral.Api/Services/Comunicaciones/EmailService.cs +++ b/Backend/GestionIntegral.Api/Services/Comunicaciones/EmailService.cs @@ -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); diff --git a/Backend/GestionIntegral.Api/appsettings.json b/Backend/GestionIntegral.Api/appsettings.json index 2748571..bce4d5f 100644 --- a/Backend/GestionIntegral.Api/appsettings.json +++ b/Backend/GestionIntegral.Api/appsettings.json @@ -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@" } } \ No newline at end of file