Exemplo n.º 1
0
        private async Task <string> ProcessEmailTemplate(PublicEnums.EmailTemplateList emailTemplate, Dictionary <string, PropertyMetaData> variableValues)
        {
            //Get email template
            string templateText = _context.EmailTemplates.Where(x => x.EventCode == emailTemplate.ToString()).First().TemplateBody;

            return(TemplateParser.TemplateParser.Render(templateText, variableValues, Placeholder.Bracket));
        }
Exemplo n.º 2
0
        public async Task <Guid> Save()
        {
            UserHelperFunctions userHelper = new UserHelperFunctions()
            {
                _context         = _context,
                _emailService    = _emailService,
                _securityOptions = _securityOptions,
                _user            = _user
            };

            userHelper.Populate();

            bool   isNew        = false;
            bool   isWasRemoved = false;
            string tempPassword = "";

            PublicEnums.EmailTemplateList emailTemplate = PublicEnums.EmailTemplateList.NTF_REGISTRATION_WELCOME_CUSTOM;

            //Save user details
            var user = _context.Users.FirstOrDefault(x => x.UserID == UserID);

            if (user == null)
            {
                //Check user not deleted before
                user = _context.Users.FirstOrDefault(x => ((x.EmailAddress == EmailAddress && x.EmailAddress != null)) && x.IsRemoved == true);
                if (user == null)
                {
                    //Perform dup-check
                    user = _context.Users.FirstOrDefault(x => ((x.EmailAddress == EmailAddress && x.EmailAddress != null)) && x.IsRemoved == false);
                    if (user == null)
                    {
                        user                 = new User();
                        isNew                = true;
                        user.UserID          = Guid.NewGuid();
                        user.IsSuspended     = false;
                        user.LoginTries      = 0;
                        user.CreatedUserID   = userHelper.loggedInUserID;
                        user.CreatedDateTime = DateTime.UtcNow;
                        user.IsRemoved       = false;

                        tempPassword = HelperFunctions.GeneratePassword(8);
                        if (!string.IsNullOrEmpty(_tmpPassword))
                        {
                            tempPassword = _tmpPassword;
                        }

                        user.Password = HashProvider.ComputeHash(tempPassword, HashProvider.HashAlgorithmList.SHA256, _securityOptions.PasswordSalt);
                        _tmpPassword  = tempPassword;
                    }
                    else
                    {
                        errorMessage = "The user email address already exists. Find the existing user first and edit their details";
                        return(Guid.Empty);
                    }
                }
                else
                {
                    tempPassword = HelperFunctions.GeneratePassword(8);
                    if (!string.IsNullOrEmpty(_tmpPassword))
                    {
                        tempPassword = _tmpPassword;
                    }

                    user.Password = HashProvider.ComputeHash(tempPassword, HashProvider.HashAlgorithmList.SHA256, _securityOptions.PasswordSalt);
                    _tmpPassword  = tempPassword;

                    user.IsRemoved = false;
                    isWasRemoved   = true;
                }
            }

            user.DisplayName  = DisplayName;
            user.EmailAddress = EmailAddress;
            user.IsSuspended  = IsSuspended;
            user.LoginTries   = (IsSuspended == false) ? 0 : user.LoginTries;
            user.EditUserID   = userHelper.loggedInUserID;
            user.EditDateTime = DateTime.UtcNow;
            user.FirstName    = FirstName;
            user.Surname      = Surname;
            user.Timezone     = SelectedTimezone;

            if (isNew)
            {
                _context.Add(user);
            }
            else
            {
                _context.Update(user);
            }

            if (isNew || isWasRemoved)
            {
                #region  Send new user registration email

                if (!string.IsNullOrEmpty(EmailAddress))
                {
                    emailTemplate = PublicEnums.EmailTemplateList.NTF_REGISTRATION_WELCOME_CUSTOM;

                    var variables = new Dictionary <string, PropertyMetaData>
                    {
                        { "HostUrl", new PropertyMetaData {
                              Type = typeof(string), Value = _securityOptions.WebsiteHostUrl
                          } },
                        { "DisplayName", new PropertyMetaData {
                              Type = typeof(string), Value = DisplayName
                          } },
                        { "Password", new PropertyMetaData {
                              Type = typeof(string), Value = tempPassword
                          } },
                        { "Username", new PropertyMetaData {
                              Type = typeof(string), Value = EmailAddress
                          } }
                    };

                    await _emailService.SendEmailAsync(new List <string>() { EmailAddress }, "Welcome", emailTemplate, variables, _user);
                }
                #endregion
            }

            await ClearUserRoles(user.UserID);
            await AddSelectedUserRoles(user.UserID);

            await _context.SaveChangesAsync();

            UserID = user.UserID;

            return(UserID);
        }
Exemplo n.º 3
0
        public async Task SendEmailAsync(List <string> toEmailAddresses, String subject,
                                         PublicEnums.EmailTemplateList emailTemplate, Dictionary <string, PropertyMetaData> variableValues,
                                         ClaimsPrincipal user, List <string> ccEmailAddresses = null, List <EmailAttachment> attachments = null)
        {
            try
            {
                if (_emailOptions.EmailEnabled)
                {
                    var emailMessage = new MimeMessage();

                    emailMessage.From.Add(new MailboxAddress(_emailOptions.FromName, _emailOptions.FromAddress));

                    foreach (var toEmailAddress in toEmailAddresses)
                    {
                        emailMessage.To.Add(new MailboxAddress("", toEmailAddress));
                    }
                    if (ccEmailAddresses != null)
                    {
                        foreach (var ccEmailAddress in ccEmailAddresses)
                        {
                            emailMessage.Cc.Add(new MailboxAddress("", ccEmailAddress));
                        }
                    }

                    //Process email template
                    string htmlMessage = await ProcessEmailTemplate(emailTemplate, variableValues);

                    emailMessage.Subject = subject;

                    //Build body
                    var builder = new BodyBuilder();
                    builder.HtmlBody = htmlMessage;

                    //Add attachments
                    if (attachments != null && attachments.Count > 0)
                    {
                        foreach (var item in attachments)
                        {
                            builder.Attachments.Add(item.AttachmentName, item.AttachmentData, ContentType.Parse(item.ContentType));
                        }
                    }

                    emailMessage.Body = builder.ToMessageBody();

                    using (var client = new SmtpClient())
                    {
                        client.LocalDomain = _emailOptions.LocalDomain;

                        await client.ConnectAsync(_emailOptions.MailServerAddress, Convert.ToInt32(_emailOptions.MailServerPort), SecureSocketOptions.Auto).ConfigureAwait(false);

                        if (_emailOptions.RequireLogin)
                        {
                            await client.AuthenticateAsync(new NetworkCredential(_emailOptions.Username, _emailOptions.UserPassword));
                        }
                        await client.SendAsync(emailMessage).ConfigureAwait(false);

                        await client.DisconnectAsync(true).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception ex)
            {
                HelperFunctions.Log(_context, PublicEnums.LogLevel.LEVEL_EXCEPTION, "Helpers.EmailServiceFactory.EmailService.SendEmailAsync", ex.Message, user, ex);
            }
        }