//readonly ConcurrentQueue<MimeMessageContainer> _emailQ = new ConcurrentQueue<MimeMessageContainer>();

        #endregion

        #region constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="EmailDispatcher"/> class using the specified parameter.
        /// </summary>
        /// <param name="emailClient">An object used to send e-mails.</param>
        /// <param name="emailSettingsProvider">An object used to obtain and refresh the e-mail configuration settings.</param>
        /// <param name="logger">An object used for logging.</param>
        /// <param name="options">The options to use for the current <see cref="IEmailDispatcher"/>.</param>
        /// <param name="sysEventRepo">The system event repository.</param>
        /// <param name="appSettingsRepo">The application settings repository.</param>
        /// <param name="sysLogger">The system event logger.</param>
        /// <param name="hubContext">The SignalR hub context.</param>
        public EmailDispatcher(IEmailClientService emailClient
                               , ILogger <EmailDispatcher> logger
                               , IOptions <MessageDispatchOptions> options
                               , IRepository <SysEventLog> sysEventRepo
                               , IRepository <AppSetting> appSettingsRepo
                               , ISysEventLogger sysLogger
                               , IHubContext <NotificationHub, INotificationHub> hubContext) : base(logger, options)
        {
            if (Instance != null)
            {
                throw new InvalidOperationException($"No more than one instance is allowed for the singleton {nameof(EmailDispatcher)} service.");
            }

            Instance = this;

            _emailClient     = emailClient;
            _sysRepo         = sysEventRepo;
            _appSettingsRepo = appSettingsRepo;
            _sysLogger       = sysLogger;
            _hubContext      = hubContext;

            SetEventHandlers()
            .Execute() // ensures that unsent e-mails stored in the database are requeued
            .StartTimer();
        }
Пример #2
0
 public AccountService(UserManager <AppUser> userManager, ITokenService tokenService, IMapper mapper,
                       IEmailClientService emailClientService, ServerSettings serverSettings)
 {
     _userManager        = userManager;
     _tokenService       = tokenService;
     _mapper             = mapper;
     _emailClientService = emailClientService;
     _serverSettings     = serverSettings;
 }
Пример #3
0
 public AccountsController(IEmailClientService emailClient
                           , IEmailConfigurationProvider emailClientSettingsFactory
                           , UserManager <AppUser> userManager
                           , ApplicationDbContext dbContext
                           , IMapper mapper
                           , IOptions <IdentityInitializerSettings> identitySettings
                           , IHttpContextAccessor httpContextAccessor
                           , ISysEventLogger sysLog
                           , IViewRenderService templateViewRender
                           , IHostingEnvironment hostingEnvironment)
     : base(userManager, dbContext, identitySettings, httpContextAccessor, sysLog)
 {
     _mapper                     = mapper;
     _templateViewRender         = templateViewRender;
     _hostingEnvironment         = hostingEnvironment;
     _emailClient                = emailClient;
     _emailClientSettingsFactory = emailClientSettingsFactory;
 }
 public EmailController(IEmailClientService emailClient, IEmailConfigurationProvider emailClientSettingsFactory)
 {
     _emailClient = emailClient;
     _emailClientSettingsFactory = emailClientSettingsFactory;
 }
Пример #5
0
        /// <summary>
        /// Asynchronously sends an e-mail on behalf of the given controller using the specified parameters.
        /// </summary>
        /// <param name="controller">The controller that initiated the action.</param>
        /// <param name="model">An object used to create the message to send.</param>
        /// <param name="emailClient">An object used to send the e-mail.</param>
        /// <returns></returns>
        public static async Task <IActionResult> SendEmailAsync(this Controller controller, EmailModel model, IEmailClientService emailClient)
        {
            var config = emailClient.Configuration;

            try
            {
                var message = EmailClientService.CreateMessage(
                    model.Subject,
                    model.Body,
                    model.From,
                    model.To
                    );

                await emailClient.SendAsync(message);

                return(controller.Ok());
            }
            catch (ServiceNotAuthenticatedException ex)
            {
                if (false == config?.RequiresAuth)
                {
                    return(controller.BadRequest(new ServiceNotAuthenticatedException(SmtpServerRequiresAuth)));
                }
                return(controller.BadRequest(ex));
            }
            catch (SslHandshakeException ex)
            {
                if (true == config?.UseSsl)
                {
                    return(controller.BadRequest(new SslHandshakeException(SmtpServerDoesNotSupportSsl)));
                }
                return(controller.BadRequest(ex));
            }
            catch (SocketException)
            {
                return(controller.BadRequest(new Exception(string.Format(SmtpHostUnreachable, config?.Host))));
            }
            catch (Exception ex)
            {
                return(controller.BadRequest(ex));
            }
        }
Пример #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EmailSender"/> class.
 /// </summary>
 /// <param name="emailClient">An object used to send emails.</param>
 /// <param name="configProvider">An object used to retrieve email configuration settings.</param>
 public EmailSender(IEmailClientService emailClient, IEmailConfigurationProvider configProvider)
 {
     Client          = emailClient ?? throw new ArgumentNullException(nameof(emailClient));
     _configProvider = configProvider ?? throw new ArgumentNullException(nameof(configProvider));
 }
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EmailSender"/> class.
 /// </summary>
 /// <param name="emailClient">An object used to send emails.</param>
 /// <param name="settingsFactory">An object used to retrieve email configuration settings.</param>
 public EmailSender(IEmailClientService emailClient, IEmailConfigurationProvider settingsFactory)
 {
     _emailClient     = emailClient;
     _settingsFactory = settingsFactory;
 }