From 9b371273b57a1f1607a26123ef2bcc3317fc2c8f Mon Sep 17 00:00:00 2001 From: elitsa Date: Mon, 27 Jan 2020 14:36:29 +0100 Subject: [PATCH] Refactoring SmtpCheck to use the new GlobalSettings to get smtp host and port instead of WebConfigurationManager --- .../Configuration/IGlobalSettings.cs | 2 ++ src/Umbraco.Configuration/GlobalSettings.cs | 28 +++++++++++++++++++ .../HealthCheck/Checks/Services/SmtpCheck.cs | 15 +++++----- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Abstractions/Configuration/IGlobalSettings.cs b/src/Umbraco.Abstractions/Configuration/IGlobalSettings.cs index 212e6f1843..df7ce0f889 100644 --- a/src/Umbraco.Abstractions/Configuration/IGlobalSettings.cs +++ b/src/Umbraco.Abstractions/Configuration/IGlobalSettings.cs @@ -67,6 +67,8 @@ string UmbracoMediaPath { get; } bool IsSmtpServerConfigured { get; } + string SmtpHost { get; } + int? SmtpPort { get; } /// /// Gets a value indicating whether the runtime should enter Install level when the database is missing. diff --git a/src/Umbraco.Configuration/GlobalSettings.cs b/src/Umbraco.Configuration/GlobalSettings.cs index 8376082c2a..9030b8a763 100644 --- a/src/Umbraco.Configuration/GlobalSettings.cs +++ b/src/Umbraco.Configuration/GlobalSettings.cs @@ -133,6 +133,34 @@ namespace Umbraco.Core.Configuration } } + public string SmtpHost + { + get + { + var networkSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp/network") as ConfigurationSection; + var host = networkSection?.ElementInformation.Properties["host"]; + if (host != null + && host.Value is string hostPropValue + && string.IsNullOrEmpty(hostPropValue) == false) + return hostPropValue; + + return null; + } + } + public int? SmtpPort + { + get + { + var networkSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp/network") as ConfigurationSection; + var port = networkSection?.ElementInformation.Properties["port"]; + if (port != null + && port.Value is int portPropValue) + return portPropValue; + + return null; + } + } + /// /// For testing only /// diff --git a/src/Umbraco.Web/HealthCheck/Checks/Services/SmtpCheck.cs b/src/Umbraco.Web/HealthCheck/Checks/Services/SmtpCheck.cs index d6f7cef497..e27bd95eb4 100644 --- a/src/Umbraco.Web/HealthCheck/Checks/Services/SmtpCheck.cs +++ b/src/Umbraco.Web/HealthCheck/Checks/Services/SmtpCheck.cs @@ -5,6 +5,7 @@ using System.Net.Configuration; using System.Net.Sockets; using System.Web.Configuration; using Umbraco.Core; +using Umbraco.Core.Configuration; using Umbraco.Core.Services; namespace Umbraco.Web.HealthCheck.Checks.Services @@ -18,11 +19,13 @@ namespace Umbraco.Web.HealthCheck.Checks.Services { private readonly ILocalizedTextService _textService; private readonly IRuntimeState _runtime; + private readonly IGlobalSettings _globalSettings; - public SmtpCheck(ILocalizedTextService textService, IRuntimeState runtime) + public SmtpCheck(ILocalizedTextService textService, IRuntimeState runtime, IGlobalSettings globalSettings) { _textService = textService; _runtime = runtime; + _globalSettings = globalSettings; } /// @@ -51,17 +54,15 @@ namespace Umbraco.Web.HealthCheck.Checks.Services var message = string.Empty; var success = false; - // appPath is the virtual application root path on the server - var config = WebConfigurationManager.OpenWebConfiguration(_runtime.ApplicationVirtualPath); - var settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings"); - if (settings == null) + if (_globalSettings.IsSmtpServerConfigured == false) { message = _textService.Localize("healthcheck/smtpMailSettingsNotFound"); } else { - var host = settings.Smtp.Network.Host; - var port = settings.Smtp.Network.Port == 0 ? DefaultSmtpPort : settings.Smtp.Network.Port; + var host = _globalSettings.SmtpHost; + var port = _globalSettings.SmtpPort ?? DefaultSmtpPort; + if (string.IsNullOrEmpty(host)) { message = _textService.Localize("healthcheck/smtpMailSettingsHostNotConfigured");