Fixed issue with the smtp settings.. Found when testing smtp healthcheck

This commit is contained in:
Bjarke Berg
2020-01-29 13:15:31 +01:00
parent cbf02d7b16
commit 14f0005345
6 changed files with 63 additions and 61 deletions
@@ -67,8 +67,7 @@
string UmbracoMediaPath { get; }
bool IsSmtpServerConfigured { get; }
string SmtpHost { get; }
int? SmtpPort { get; }
ISmtpSettings SmtpSettings { get; }
/// <summary>
/// Gets a value indicating whether the runtime should enter Install level when the database is missing.
@@ -0,0 +1,10 @@
namespace Umbraco.Core.Configuration
{
public interface ISmtpSettings
{
string From { get; }
string Host { get; }
int Port{ get; }
string PickupDirectoryLocation { get; }
}
}
@@ -48,29 +48,27 @@ namespace Umbraco.Web.HealthCheck.Checks.Services
private HealthCheckStatus CheckSmtpSettings()
{
const int DefaultSmtpPort = 25;
var message = string.Empty;
var success = false;
if (_globalSettings.IsSmtpServerConfigured == false)
var smtpSettings = _globalSettings.SmtpSettings;
if (smtpSettings == null)
{
message = _textService.Localize("healthcheck/smtpMailSettingsNotFound");
}
else
{
var host = _globalSettings.SmtpHost;
var port = _globalSettings.SmtpPort ?? DefaultSmtpPort;
if (string.IsNullOrEmpty(host))
if (string.IsNullOrEmpty(smtpSettings.Host))
{
message = _textService.Localize("healthcheck/smtpMailSettingsHostNotConfigured");
}
else
{
success = CanMakeSmtpConnection(host, port);
success = CanMakeSmtpConnection(smtpSettings.Host, smtpSettings.Port);
message = success
? _textService.Localize("healthcheck/smtpMailSettingsConnectionSuccess")
: _textService.Localize("healthcheck/smtpMailSettingsConnectionFail", new [] { host, port.ToString() });
: _textService.Localize("healthcheck/smtpMailSettingsConnectionFail", new [] { smtpSettings.Host, smtpSettings.Port.ToString() });
}
}
+34 -48
View File
@@ -1,7 +1,9 @@
using System;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Xml.Linq;
using Umbraco.Configuration;
using Umbraco.Core.IO;
namespace Umbraco.Core.Configuration
@@ -84,7 +86,6 @@ namespace Umbraco.Core.Configuration
{
_reservedPaths = null;
_reservedUrls = null;
HasSmtpServer = null;
}
/// <summary>
@@ -95,29 +96,39 @@ namespace Umbraco.Core.Configuration
{
ResetInternal();
}
public bool IsSmtpServerConfigured
{
get
{
var smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as ConfigurationSection;
if (smtpSection is null) return false;
var smtpSettings = SmtpSettings;
if (smtpSettings is null) return false;
if (!(smtpSettings.From is null)) return true;
if (!(smtpSettings.Host is null)) return true;
if (!(smtpSettings.PickupDirectoryLocation is null)) return true;
return false;
}
}
public ISmtpSettings SmtpSettings
{
get
{
var smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as ConfigurationSection;
if (smtpSection is null) return null;
var result = new SmtpSettings();
var from = smtpSection.ElementInformation.Properties["from"];
if (@from != null
&& @from.Value is string fromPropValue
&& string.IsNullOrEmpty(fromPropValue) == false
&& !string.Equals("noreply@example.com", fromPropValue, StringComparison.OrdinalIgnoreCase))
{
return true;
}
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 true;
result.From = fromPropValue;
}
var specifiedPickupDirectorySection = ConfigurationManager.GetSection("system.net/mailSettings/smtp/specifiedPickupDirectory") as ConfigurationSection;
@@ -126,46 +137,21 @@ namespace Umbraco.Core.Configuration
&& pickupDirectoryLocation.Value is string pickupDirectoryLocationPropValue
&& string.IsNullOrEmpty(pickupDirectoryLocationPropValue) == false)
{
return true;
result.PickupDirectoryLocation = pickupDirectoryLocationPropValue;
}
return false;
// SmtpClient can magically read the section system.net/mailSettings/smtp/network, witch is always
// null if we use ConfigurationManager.GetSection. SmtpSection does not exist in .Net Standard
var smtpClient = new SmtpClient();
result.Host = smtpClient.Host;
result.Port = smtpClient.Port;
return result;
}
}
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>
internal static bool? HasSmtpServer { get; set; }
/// <summary>
/// Gets the reserved urls from web.config.
/// </summary>
+12
View File
@@ -0,0 +1,12 @@
using Umbraco.Core.Configuration;
namespace Umbraco.Configuration
{
public class SmtpSettings : ISmtpSettings
{
public string From { get; set; }
public string Host { get; set; }
public int Port { get; set; }
public string PickupDirectoryLocation { get; set; }
}
}
@@ -63,9 +63,6 @@ namespace Umbraco.Tests.Web.Controllers
{
ApiController CtrlFactory(HttpRequestMessage message, IUmbracoContextAccessor umbracoContextAccessor, UmbracoHelper helper)
{
//setup some mocks
Umbraco.Core.Configuration.GlobalSettings.HasSmtpServer = true;
var userServiceMock = Mock.Get(Current.Services.UserService);
userServiceMock.Setup(service => service.Save(It.IsAny<IUser>(), It.IsAny<bool>()))