Refactoring SmtpCheck to use the new GlobalSettings to get smtp host and port instead of WebConfigurationManager

This commit is contained in:
elitsa
2020-01-27 14:36:29 +01:00
parent 770c4beb95
commit 9b371273b5
3 changed files with 38 additions and 7 deletions

View File

@@ -67,6 +67,8 @@
string UmbracoMediaPath { get; }
bool IsSmtpServerConfigured { get; }
string SmtpHost { get; }
int? SmtpPort { get; }
/// <summary>
/// Gets a value indicating whether the runtime should enter Install level when the database is missing.

View File

@@ -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;
}
}
/// <summary>
/// For testing only
/// </summary>

View File

@@ -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;
}
/// <summary>
@@ -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");