2017-09-08 19:39:13 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Net.Mail;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Configuration;
|
2018-11-22 14:05:51 +00:00
|
|
|
|
using Umbraco.Core.Configuration.HealthChecks;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.HealthCheck.NotificationMethods
|
|
|
|
|
|
{
|
|
|
|
|
|
[HealthCheckNotificationMethod("email")]
|
2018-11-22 14:05:51 +00:00
|
|
|
|
public class EmailNotificationMethod : NotificationMethodBase, IHealthCheckNotificatationMethod
|
2017-09-08 19:39:13 +02:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ILocalizedTextService _textService;
|
|
|
|
|
|
|
2018-11-22 14:05:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Default constructor which is used in the provider model
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="enabled"></param>
|
|
|
|
|
|
/// <param name="failureOnly"></param>
|
|
|
|
|
|
/// <param name="verbosity"></param>
|
|
|
|
|
|
/// <param name="recipientEmail"></param>
|
|
|
|
|
|
public EmailNotificationMethod(bool enabled, bool failureOnly, HealthCheckNotificationVerbosity verbosity,
|
|
|
|
|
|
string recipientEmail)
|
|
|
|
|
|
: this(enabled, failureOnly, verbosity, recipientEmail, ApplicationContext.Current.Services.TextService)
|
2017-09-08 19:39:13 +02:00
|
|
|
|
{
|
2018-11-22 14:05:51 +00:00
|
|
|
|
}
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
2018-11-22 14:05:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor that could be used for testing
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="enabled"></param>
|
|
|
|
|
|
/// <param name="failureOnly"></param>
|
|
|
|
|
|
/// <param name="verbosity"></param>
|
|
|
|
|
|
/// <param name="recipientEmail"></param>
|
|
|
|
|
|
/// <param name="textService"></param>
|
|
|
|
|
|
internal EmailNotificationMethod(bool enabled, bool failureOnly, HealthCheckNotificationVerbosity verbosity,
|
|
|
|
|
|
string recipientEmail, ILocalizedTextService textService)
|
|
|
|
|
|
: base(enabled, failureOnly, verbosity)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (textService == null) throw new ArgumentNullException("textService");
|
|
|
|
|
|
_textService = textService;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
RecipientEmail = recipientEmail;
|
2018-11-22 14:05:51 +00:00
|
|
|
|
Verbosity = verbosity;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-22 14:05:51 +00:00
|
|
|
|
public string RecipientEmail { get; private set; }
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
2018-11-22 14:05:51 +00:00
|
|
|
|
public async Task SendAsync(HealthCheckResults results)
|
2017-09-08 19:39:13 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (ShouldSend(results) == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(RecipientEmail))
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var message = _textService.Localize("healthcheck/scheduledHealthCheckEmailBody", new[]
|
|
|
|
|
|
{
|
|
|
|
|
|
DateTime.Now.ToShortDateString(),
|
|
|
|
|
|
DateTime.Now.ToShortTimeString(),
|
|
|
|
|
|
results.ResultsAsHtml(Verbosity)
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var subject = _textService.Localize("healthcheck/scheduledHealthCheckEmailSubject");
|
|
|
|
|
|
|
2017-09-19 15:51:47 +02:00
|
|
|
|
var mailSender = new EmailSender();
|
2018-11-22 14:05:51 +00:00
|
|
|
|
using (var mailMessage = new MailMessage(UmbracoConfig.For.UmbracoSettings().Content.NotificationEmailAddress,
|
|
|
|
|
|
RecipientEmail,
|
|
|
|
|
|
string.IsNullOrEmpty(subject) ? "Umbraco Health Check Status" : subject,
|
|
|
|
|
|
message)
|
|
|
|
|
|
{
|
|
|
|
|
|
IsBodyHtml = message.IsNullOrWhiteSpace() == false
|
|
|
|
|
|
&& message.Contains("<") && message.Contains("</")
|
|
|
|
|
|
})
|
2017-09-08 19:39:13 +02:00
|
|
|
|
{
|
2017-09-19 15:51:47 +02:00
|
|
|
|
await mailSender.SendAsync(mailMessage);
|
2017-09-08 19:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|