<commit_before>// Copyright (c) Umbraco.
// See LICENSE for more details.

using System.Net.Mail;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Mail;
using Umbraco.Cms.Core.Models.Email;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Infrastructure.Extensions;
using SmtpClient = MailKit.Net.Smtp.SmtpClient;

namespace Umbraco.Cms.Infrastructure
{
    /// <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!
        private readonly IEventAggregator _eventAggregator;
        private readonly GlobalSettings _globalSettings;
        private readonly bool _notificationHandlerRegistered;
        private readonly ILogger<EmailSender> _logger;

        public EmailSender(
            ILogger<EmailSender> logger,
            IOptions<GlobalSettings> globalSettings,
            IEventAggregator eventAggregator)
            : this(globalSettings, eventAggregator, null)
            => _logger = logger;

        public EmailSender(IOptions<GlobalSettings> globalSettings, IEventAggregator eventAggregator, INotificationHandler<SendEmailNotification> handler)
        {
            _eventAggregator = eventAggregator;
            _globalSettings = globalSettings.Value;
            _notificationHandlerRegistered = handler is not null;
        }

        /// <summary>
        /// Sends the message async
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task SendAsync(EmailMessage message) => await SendAsyncInternal(message, false);

        public async Task SendAsync(EmailMessage message, bool enableNotification) =>
            await SendAsyncInternal(message, enableNotification);

        private async Task SendAsyncInternal(EmailMessage message, bool enableNotification)
        {
            if (enableNotification)
            {
                var notification = new SendEmailNotification(message.ToNotificationEmail(_globalSettings.Smtp?.From));
                await _eventAggregator.PublishAsync(notification);

                // if a handler handled sending the email then don't continue.
                if (notification.IsHandled)
                {
                    _logger.LogDebug("The email sending for {Subject} was handled by a notification handler", notification.Message.Subject);
                    return;<commit_msg>Fix issue where MSDI confused which ctor to use for EmailSender<commit_after>// Copyright (c) Umbraco.
// See LICENSE for more details.

using System.Net.Mail;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Mail;
using Umbraco.Cms.Core.Models.Email;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Infrastructure.Extensions;
using SmtpClient = MailKit.Net.Smtp.SmtpClient;

namespace Umbraco.Cms.Infrastructure
{
    /// <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!
        private readonly IEventAggregator _eventAggregator;
        private readonly GlobalSettings _globalSettings;
        private readonly bool _notificationHandlerRegistered;
        private readonly ILogger<EmailSender> _logger;

        public EmailSender(
            ILogger<EmailSender> logger,
            IOptions<GlobalSettings> globalSettings,
            IEventAggregator eventAggregator)
            : this(logger, globalSettings, eventAggregator, null) { }

        public EmailSender(
            ILogger<EmailSender> logger,
            IOptions<GlobalSettings> globalSettings,
            IEventAggregator eventAggregator,
            INotificationHandler<SendEmailNotification> handler)
        {
            _logger = logger;
            _eventAggregator = eventAggregator;
            _globalSettings = globalSettings.Value;
            _notificationHandlerRegistered = handler is not null;
        }

        /// <summary>
        /// Sends the message async
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task SendAsync(EmailMessage message) => await SendAsyncInternal(message, false);

        public async Task SendAsync(EmailMessage message, bool enableNotification) =>
            await SendAsyncInternal(message, enableNotification);

        private async Task SendAsyncInternal(EmailMessage message, bool enableNotification)
        {
            if (enableNotification)
            {
                var notification = new SendEmailNotification(message.ToNotificationEmail(_globalSettings.Smtp?.From));
                await _eventAggregator.PublishAsync(notification);

                // if a handler handled sending the email then don't continue.
                if (notification.IsHandled)
                {
                    _logger.LogDebug("The email sending for {Subject} was handled by a notification handler", notification.Message.Subject);
                    return;
{'fraction_non_alphanumeric': 0.07722007722007722, 'fraction_numerical': 0.0, 'mean_word_length': 2.6956521739130435, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2389150', 'n_tokens_mistral': 1327, 'n_tokens_neox': 1326, 'n_words': 355}