81 lines
2.5 KiB
C#
81 lines
2.5 KiB
C#
|
|
using System;
|
|||
|
|
using System.Net.Mail;
|
|||
|
|
using System.Threading;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Umbraco.Core;
|
|||
|
|
using Umbraco.Core.Configuration;
|
|||
|
|
using Umbraco.Core.Services;
|
|||
|
|
|
|||
|
|
namespace Umbraco.Web.HealthCheck.NotificationMethods
|
|||
|
|
{
|
|||
|
|
[HealthCheckNotificationMethod("email")]
|
|||
|
|
public class EmailNotificationMethod : NotificationMethodBase
|
|||
|
|
{
|
|||
|
|
private readonly ILocalizedTextService _textService;
|
|||
|
|
|
|||
|
|
internal EmailNotificationMethod(ILocalizedTextService textService)
|
|||
|
|
{
|
|||
|
|
var recipientEmail = Settings["recipientEmail"]?.Value;
|
|||
|
|
if (string.IsNullOrWhiteSpace(recipientEmail))
|
|||
|
|
{
|
|||
|
|
Enabled = false;
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
RecipientEmail = recipientEmail;
|
|||
|
|
|
|||
|
|
_textService = textService ?? throw new ArgumentNullException(nameof(textService));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public string RecipientEmail { get; }
|
|||
|
|
|
|||
|
|
public override async Task SendAsync(HealthCheckResults results, CancellationToken token)
|
|||
|
|
{
|
|||
|
|
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");
|
|||
|
|
|
|||
|
|
using (var client = new SmtpClient())
|
|||
|
|
using (var mailMessage = CreateMailMessage(subject, message))
|
|||
|
|
{
|
|||
|
|
if (client.DeliveryMethod == SmtpDeliveryMethod.Network)
|
|||
|
|
{
|
|||
|
|
await client.SendMailAsync(mailMessage);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
client.Send(mailMessage);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private MailMessage CreateMailMessage(string subject, string message)
|
|||
|
|
{
|
|||
|
|
var to = UmbracoConfig.For.UmbracoSettings().Content.NotificationEmailAddress;
|
|||
|
|
|
|||
|
|
if (string.IsNullOrWhiteSpace(subject))
|
|||
|
|
subject = "Umbraco Health Check Status";
|
|||
|
|
|
|||
|
|
return new MailMessage(to, RecipientEmail, subject, message)
|
|||
|
|
{
|
|||
|
|
IsBodyHtml = message.IsNullOrWhiteSpace() == false && message.Contains("<") && message.Contains("</")
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|