public async Task AccountPendingApprovalAdminNotification(
            ISiteContext siteSettings,
            ISiteUser user)
        {
            if (siteSettings.AccountApprovalEmailCsv.Length == 0)
            {
                return;
            }

            SmtpOptions smtpOptions = GetSmptOptions(siteSettings);

            if (smtpOptions == null)
            {
                var logMessage = $"failed to send new account notifications to admins because smtp settings are not populated for site {siteSettings.SiteName}";
                log.LogError(logMessage);
                return;
            }

            string subject = sr["New Account Pending Approval"];

            EmailSender sender = new EmailSender();

            try
            {
                var plainTextMessage
                    = await viewRenderer.RenderViewAsString <ISiteUser>("EmailTemplates/AccountPendingApprovalAdminNotificationTextEmail", user).ConfigureAwait(false);

                var htmlMessage
                    = await viewRenderer.RenderViewAsString <ISiteUser>("EmailTemplates/AccountPendingApprovalAdminNotificationHtmlEmail", user).ConfigureAwait(false);

                await sender.SendMultipleEmailAsync(
                    smtpOptions,
                    siteSettings.AccountApprovalEmailCsv,
                    smtpOptions.DefaultEmailFromAddress,
                    subject,
                    plainTextMessage,
                    htmlMessage).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                log.LogError("error sending email verification email", ex);
            }
        }
        public async Task SendAccountApprovalNotificationAsync(
            ISiteContext siteSettings,
            string toAddress,
            string subject,
            string loginUrl)
        {
            SmtpOptions smtpOptions = GetSmptOptions(siteSettings);

            if (smtpOptions == null)
            {
                var logMessage = $"failed to send account approval email because smtp settings are not populated for site {siteSettings.SiteName}";
                log.LogError(logMessage);
                return;
            }

            EmailSender sender = new EmailSender();

            // in account controller we are calling this method without await
            // so it doesn't block the UI. Which means it is running on a background thread
            // similar as the old ThreadPool.QueueWorkItem
            // as such we need to handle any error that may happen so it doesn't
            // brind down the thread or the process
            try
            {
                var plainTextMessage
                    = await viewRenderer.RenderViewAsString <string>("EmailTemplates/AccountApprovedTextEmail", loginUrl);

                var htmlMessage
                    = await viewRenderer.RenderViewAsString <string>("EmailTemplates/AccountApprovedHtmlEmail", loginUrl);

                await sender.SendEmailAsync(
                    smtpOptions,
                    toAddress,
                    smtpOptions.DefaultEmailFromAddress,
                    subject,
                    plainTextMessage,
                    htmlMessage).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                log.LogError("error sending password reset email", ex);
            }
        }
Esempio n. 3
0
        private SmtpOptions GetSmptOptions(ISiteSettings siteSettings)
        {
            if (string.IsNullOrWhiteSpace(siteSettings.SmtpServer))
            {
                return(null);
            }

            SmtpOptions smtpOptions = new SmtpOptions();

            smtpOptions.Password               = siteSettings.SmtpPassword;
            smtpOptions.Port                   = siteSettings.SmtpPort;
            smtpOptions.PreferredEncoding      = siteSettings.SmtpPreferredEncoding;
            smtpOptions.RequiresAuthentication = siteSettings.SmtpRequiresAuth;
            smtpOptions.Server                 = siteSettings.SmtpServer;
            smtpOptions.User                   = siteSettings.SmtpUser;
            smtpOptions.UseSsl                 = siteSettings.SmtpUseSsl;

            return(smtpOptions);
        }
        private SmtpOptions GetSmptOptions(ISiteContext siteSettings)
        {
            if (!siteSettings.SmtpIsConfigured())
            {
                return(globalSmtpSettings);
            }

            SmtpOptions smtpOptions = new SmtpOptions();

            smtpOptions.Password                = siteSettings.SmtpPassword;
            smtpOptions.Port                    = siteSettings.SmtpPort;
            smtpOptions.PreferredEncoding       = siteSettings.SmtpPreferredEncoding;
            smtpOptions.RequiresAuthentication  = siteSettings.SmtpRequiresAuth;
            smtpOptions.Server                  = siteSettings.SmtpServer;
            smtpOptions.User                    = siteSettings.SmtpUser;
            smtpOptions.UseSsl                  = siteSettings.SmtpUseSsl;
            smtpOptions.DefaultEmailFromAddress = siteSettings.DefaultEmailFromAddress;
            smtpOptions.DefaultEmailFromAlias   = siteSettings.DefaultEmailFromAlias;

            return(smtpOptions);
        }
        public Task <SmtpOptions> GetSmtpOptions()
        {
            if (!currentSite.SmtpIsConfigured())
            {
                return(Task.FromResult(globalSmtp));
            }

            SmtpOptions smtpOptions = new SmtpOptions();

            smtpOptions.Password                = currentSite.SmtpPassword;
            smtpOptions.Port                    = currentSite.SmtpPort;
            smtpOptions.PreferredEncoding       = currentSite.SmtpPreferredEncoding;
            smtpOptions.RequiresAuthentication  = currentSite.SmtpRequiresAuth;
            smtpOptions.Server                  = currentSite.SmtpServer;
            smtpOptions.User                    = currentSite.SmtpUser;
            smtpOptions.UseSsl                  = currentSite.SmtpUseSsl;
            smtpOptions.DefaultEmailFromAddress = currentSite.DefaultEmailFromAddress;
            smtpOptions.DefaultEmailFromAlias   = currentSite.DefaultEmailFromAlias;

            return(Task.FromResult(smtpOptions));
        }
Esempio n. 6
0
        public async Task SendSecurityCodeEmailAsync(
            ISiteSettings siteSettings,
            string toAddress,
            string subject,
            string securityCode)
        {
            SmtpOptions smtpOptions = GetSmptOptions(siteSettings);

            if (smtpOptions == null)
            {
                var logMessage = $"failed to send security code email because smtp settings are not populated for site {siteSettings.SiteName}";
                log.LogError(logMessage);
                return;
            }

            string plainTextTemplate = templateService.GetPlainTextTemplate(MessagePurpose.SendSecurityCode, CultureInfo.CurrentUICulture.Name);
            string plainTextMessage  = string.Format(plainTextTemplate, securityCode);

            string htmlTemplate = templateService.GetHtmlTemplate(MessagePurpose.SendSecurityCode, CultureInfo.CurrentUICulture.Name);
            string htmlMessage  = string.Format(htmlTemplate, securityCode);


            EmailSender sender = new EmailSender();

            try
            {
                await sender.SendEmailAsync(
                    smtpOptions,
                    toAddress,
                    siteSettings.DefaultEmailFromAddress,
                    subject,
                    plainTextMessage,
                    htmlMessage).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                log.LogError("error sending security code email", ex);
            }
        }
        public async Task SendAccountConfirmationEmailAsync(
            ISiteContext siteSettings,
            string toAddress,
            string subject,
            string confirmationUrl)
        {
            SmtpOptions smtpOptions = GetSmptOptions(siteSettings);

            if (smtpOptions == null)
            {
                var logMessage = $"failed to send account confirmation email because smtp settings are not populated for site {siteSettings.SiteName}";
                log.LogError(logMessage);
                return;
            }

            EmailSender sender = new EmailSender();

            try
            {
                var plainTextMessage
                    = await viewRenderer.RenderViewAsString <string>("EmailTemplates/ConfirmAccountTextEmail", confirmationUrl).ConfigureAwait(false);

                var htmlMessage
                    = await viewRenderer.RenderViewAsString <string>("EmailTemplates/ConfirmAccountHtmlEmail", confirmationUrl).ConfigureAwait(false);

                await sender.SendEmailAsync(
                    smtpOptions,
                    toAddress,
                    smtpOptions.DefaultEmailFromAddress,
                    subject,
                    plainTextMessage,
                    htmlMessage).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                log.LogError("error sending account confirmation email", ex);
            }
        }
Esempio n. 8
0
        public async Task SendAccountConfirmationEmailAsync(
            ISiteSettings siteSettings,
            string toAddress,
            string subject,
            string confirmationUrl)
        {
            SmtpOptions smtpOptions = GetSmptOptions(siteSettings);

            string plainTextTemplate = templateService.GetPlainTextTemplate(MessagePurpose.ConfirmAccount, "");
            string plainTextMessage  = string.Format(plainTextTemplate, confirmationUrl);

            string htmlTemplate = templateService.GetHtmlTemplate(MessagePurpose.ConfirmAccount, "");
            string htmlMessage  = string.Format(htmlTemplate, confirmationUrl);

            EmailSender sender = new EmailSender();
            await sender.SendEmailAsync(
                smtpOptions,
                toAddress,
                siteSettings.DefaultEmailFromAddress,
                subject,
                plainTextMessage,
                htmlMessage);
        }
        public EmailSenderFactory(IConfiguration configuration, IServiceProvider serviceProvider)
        {
            var emailType = configuration["EmailSender:EmailSenderType"].ToLower();

            if (emailType == "pickup")
            {
                var options = new PickFolderSmtpOptions();
                configuration.GetSection("EmailSender").Bind(options);
                _emailSender = new PickFolderEmailSender(options);
            }
            else if (emailType == "smtp")
            {
                var options = new SmtpOptions();
                configuration.GetSection("EmailSender").Bind(options);
                _emailSender = new SmtpEmailSender(options);
            }
            else
            {
                var options = new SendGridOptions();
                configuration.GetSection("EmailSender").Bind(options);
                var logger = ActivatorUtilities.GetServiceOrCreateInstance <ILogger <SendGridEmailSender> >(serviceProvider);
                _emailSender = new SendGridEmailSender(options, logger);
            }
        }
Esempio n. 10
0
 /// <summary>
 ///     Create new instance.
 /// </summary>
 public SmtpService(IOptions <SmtpOptions> options, ILogger <SmtpService> logger)
 {
     _options = options?.Value ?? throw new ArgumentNullException(nameof(options));
     Logger   = logger ?? throw new ArgumentNullException(nameof(logger));
 }
Esempio n. 11
0
 public SmtpEmailService(IApplicationLocalizer localizer, ILogger <SmtpEmailService> logger, SmtpOptions smtpOptions)
 {
     _localizer   = localizer;
     _logger      = logger;
     _smtpOptions = smtpOptions;
 }
Esempio n. 12
0
 public EmailSender(IHostingEnvironment hostingEnvironment, IOptions <SmtpOptions> smtpOptions)
 {
     _hostingEnvironment = hostingEnvironment;
     _smtpOptions        = smtpOptions.Value;
 }
Esempio n. 13
0
 public EmailService(SmtpOptions smtpOptions)
 {
     this.smtpOptions = smtpOptions;
 }
Esempio n. 14
0
 public EmailSender(IOptions <SmtpOptions> options, ILogger <EmailSender> logger)
 {
     _options = options.Value;
     _logger  = logger;
 }
Esempio n. 15
0
 public void TestInitialize()
 {
     _options = new SmtpOptions("smtp.domain.com", 1, "*****@*****.**", "Jhon Doe", "D", "E", false);
     _client  = Substitute.For <ISmtpClient>();
     _service = new MailService(_client, _options);
 }
Esempio n. 16
0
 public SmtpSettingsModel(SmtpOptions settings)
     : base(settings)
 {
     ConfirmSmtpPassword = SmtpPassword;
 }
 public EmailInfrastructureService(IConfiguration configuration)
 {
     _smtpOptions = new SmtpOptions();
     configuration.GetSection(SmtpOptions.SectionName).Bind(_smtpOptions);
 }
 public MailKitMessagingService(IOptions <SmtpOptions> smtpOptionsAccessor, ILogger <MailKitMessagingService> logger)
 {
     _logger      = logger ?? throw new ArgumentNullException(nameof(logger));
     _smtpOptions = smtpOptionsAccessor?.Value ?? throw new ArgumentNullException(nameof(smtpOptionsAccessor));
 }
Esempio n. 19
0
 public SmtpEffect(IOptions <SmtpOptions> options, EmailService emailService)
 {
     _emailService = emailService;
     _smtpOptions  = options.Value;
 }
Esempio n. 20
0
 public EmailSender(ILogger <EmailSender> logger, IOptions <ConfigurationModel> options)
 {
     this._logger  = logger;
     this._options = options.Value.Smtp;
 }
Esempio n. 21
0
 public EmailProvider(IConfiguration configuration, IOptions <SmtpOptions> smtpOptions)
 {
     _smtpOptions = smtpOptions.Value;
 }
Esempio n. 22
0
        /// <summary>
        /// Send email to single email id
        /// </summary>
        public async Task SendSingleEmailAsync(
            SmtpOptions smtpOptions,
            string to,
            string from,
            string subject,
            string plainTextMessage,
            string htmlMessage,
            string replyTo = null)
        {
            if (string.IsNullOrWhiteSpace(to))
            {
                throw new ArgumentException("no to address provided");
            }

            if (string.IsNullOrWhiteSpace(from))
            {
                throw new ArgumentException("no from address provided");
            }

            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentException("no subject provided");
            }

            var hasPlainText = !string.IsNullOrWhiteSpace(plainTextMessage);
            var hasHtml      = !string.IsNullOrWhiteSpace(htmlMessage);

            if (!hasPlainText && !hasHtml)
            {
                throw new ArgumentException("no message provided");
            }

            var m = new MimeMessage();

            m.From.Add(new MailboxAddress("", from));
            if (!string.IsNullOrWhiteSpace(replyTo))
            {
                m.ReplyTo.Add(new MailboxAddress("", replyTo));
            }
            m.To.Add(new MailboxAddress("", to));
            m.Subject = subject;

            //m.Importance = MessageImportance.Normal;
            //Header h = new Header(HeaderId.Precedence, "Bulk");
            //m.Headers.Add()

            BodyBuilder bodyBuilder = new BodyBuilder();

            if (hasPlainText)
            {
                bodyBuilder.TextBody = plainTextMessage;
            }

            if (hasHtml)
            {
                bodyBuilder.HtmlBody = htmlMessage;
            }

            m.Body = bodyBuilder.ToMessageBody();

            try
            {
                using (var client = new SmtpClient())
                {
                    await client.ConnectAsync(
                        smtpOptions.Server,
                        smtpOptions.Port,
                        smtpOptions.UseSsl)
                    .ConfigureAwait(false);

                    // Note: since we don't have an OAuth2 token, disable
                    // the XOAUTH2 authentication mechanism.
                    client.AuthenticationMechanisms.Remove("XOAUTH2");

                    // Note: only needed if the SMTP server requires authentication
                    if (smtpOptions.RequiresAuthentication)
                    {
                        await client.AuthenticateAsync(smtpOptions.User, smtpOptions.Password)
                        .ConfigureAwait(false);
                    }

                    await client.SendAsync(m).ConfigureAwait(false);

                    await client.DisconnectAsync(true).ConfigureAwait(false);
                }
                _logger.Trace("Send Mail Successfully to {to}.", to);
            }
            catch (Exception e)
            {
                _logger.Trace("Send Mail failed to {to}.", to);
                _logger.Error(e, "An error occurred while sending the email request.");
            }
        }
Esempio n. 23
0
 public EmailService(IOptions <SmtpOptions> options)
 {
     Options = options.Value;
 }
        public static PasswordlessLoginBuilder AddSmtpEmail(this PasswordlessLoginBuilder builder, SmtpOptions smtpOptions)
        {
            builder.Services.AddSingleton(smtpOptions);
            builder.Services.AddTransient <IEmailService, SmtpEmailService>();

            return(builder);
        }
Esempio n. 25
0
 public EmailSenderGrain(IOptions <SmtpOptions> smtpOptionsAccessor, ILogger <EmailSenderGrain> logger)
 {
     _smtpOptions = smtpOptionsAccessor.Value;
     _logger      = logger;
 }
Esempio n. 26
0
 public EmailService(IOptions <SmtpOptions> smtpOptions)
 {
     _smtpOptions = smtpOptions.Value;
 }
Esempio n. 27
0
 public static EmailServiceBuilder AddSmtpClient(this EmailServiceBuilder builder, SmtpOptions options)
 => AddSmtpClient(builder, _ => options);
Esempio n. 28
0
 public EmailProvider(IOptions <SmtpOptions> options)
 {
     _options = options.Value;
 }
Esempio n. 29
0
 public SendEmail(ISmtpService smtpService, IOptions <SmtpOptions> options)
 {
     _smtpService = smtpService;
     _options     = options.Value;
 }
Esempio n. 30
0
 public Messaging(SmsOptions smsOptions, SmtpOptions smtpOptions, ILogger <Messaging> logger)
 {
     _smsOptions  = smsOptions;
     _smtpOptions = smtpOptions;
     _logger      = logger;
 }