Files
Umbraco-CMS/src/Umbraco.Core/EmailSender.cs

108 lines
3.3 KiB
C#
Raw Normal View History

2017-09-15 18:22:19 +02:00
using System;
using System.Net.Mail;
using System.Threading.Tasks;
using Umbraco.Core.Composing;
2019-12-06 12:09:47 +01:00
using Umbraco.Core.Configuration;
2017-09-15 18:22:19 +02:00
using Umbraco.Core.Events;
namespace Umbraco.Core
{
/// <summary>
/// A utility class for sending emails
/// </summary>
public class EmailSender : IEmailSender
{
// TODO: This should encapsulate a BackgroundTaskRunner with a queue to send these emails!
2017-09-15 18:22:19 +02:00
2019-12-06 12:09:47 +01:00
private readonly IGlobalSettings _globalSettings;
2017-09-15 18:22:19 +02:00
private readonly bool _enableEvents;
2019-12-06 12:09:47 +01:00
public EmailSender(IGlobalSettings globalSettings) : this(globalSettings, false)
2017-09-15 18:22:19 +02:00
{
}
2019-12-06 12:09:47 +01:00
internal EmailSender(IGlobalSettings globalSettings, bool enableEvents)
2017-09-15 18:22:19 +02:00
{
2019-12-06 12:09:47 +01:00
_globalSettings = globalSettings;
2017-09-15 18:22:19 +02:00
_enableEvents = enableEvents;
2019-12-06 12:09:47 +01:00
_smtpConfigured = new Lazy<bool>(() => _globalSettings.IsSmtpServerConfigured);
2017-09-15 18:22:19 +02:00
}
2019-12-06 12:09:47 +01:00
private readonly Lazy<bool> _smtpConfigured;
2017-09-15 18:22:19 +02:00
/// <summary>
/// Sends the message non-async
/// </summary>
/// <param name="message"></param>
public void Send(MailMessage message)
{
2019-12-06 12:09:47 +01:00
if (_smtpConfigured.Value == false && _enableEvents)
2017-09-15 18:22:19 +02:00
{
OnSendEmail(new SendEmailEventArgs(message));
}
else
{
using (var client = new SmtpClient())
{
client.Send(message);
}
}
}
/// <summary>
/// Sends the message async
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task SendAsync(MailMessage message)
{
2019-12-06 12:09:47 +01:00
if (_smtpConfigured.Value == false && _enableEvents)
2017-09-15 18:22:19 +02:00
{
OnSendEmail(new SendEmailEventArgs(message));
}
else
{
using (var client = new SmtpClient())
{
if (client.DeliveryMethod == SmtpDeliveryMethod.Network)
{
await client.SendMailAsync(message);
}
else
{
client.Send(message);
}
}
2017-09-23 10:08:18 +02:00
}
2017-09-15 18:22:19 +02:00
}
/// <summary>
/// Returns true if the application should be able to send a required application email
/// </summary>
/// <remarks>
/// We assume this is possible if either an event handler is registered or an smtp server is configured
/// </remarks>
2019-12-06 12:09:47 +01:00
internal static bool CanSendRequiredEmail(IGlobalSettings globalSettings) => EventHandlerRegistered || globalSettings.IsSmtpServerConfigured;
2017-09-15 18:22:19 +02:00
/// <summary>
/// returns true if an event handler has been registered
/// </summary>
internal static bool EventHandlerRegistered
{
get { return SendEmail != null; }
}
2017-09-23 10:08:18 +02:00
2017-09-15 18:22:19 +02:00
/// <summary>
/// An event that is raised when no smtp server is configured if events are enabled
/// </summary>
internal static event EventHandler<SendEmailEventArgs> SendEmail;
private static void OnSendEmail(SendEmailEventArgs e)
{
var handler = SendEmail;
if (handler != null) handler(null, e);
}
}
}