Exemplo n.º 1
0
        public async Task <JsonResult> SendPasswordResetEmail(UserViewModel viewModel)
        {
            var emailSuccessful = "";
            var userExists      = landingPageService.DoesUserExist(viewModel.UserName);

            if (string.IsNullOrEmpty(viewModel.UserName) || !userExists)
            {
                emailSuccessful = "fail";
                return(new JsonResult {
                    Data = emailSuccessful
                });
            }
            var user    = landingPageService.LookupUser(viewModel.UserName);
            var message = new MailMessage();
            var body    = PartialHelper.RenderViewToString(ControllerContext, "PasswordResetEmail", viewModel);

            try
            {
                message.To.Add(new MailAddress(user.EmailAddress));
                message.Subject    = "Password Reset from devinmajor.com";
                message.Body       = body;
                message.IsBodyHtml = true;
                var emailModel = new ContactEmailViewModel()
                {
                    UserGUID       = viewModel.GUID,
                    Content        = body,
                    RecipientEmail = viewModel.EmailAddress,
                    RecipientName  = viewModel.UserName,
                    EmailTypeID    = (int)EmailType.EmailTypes.PasswordReset,
                    Subject        = "Password Reset from devinmajor.com"
                };
                using (var smtp = new SmtpClient())
                {
                    await smtp.SendMailAsync(message);
                }
                landingPageService.EmailSent(emailModel);
                return(new JsonResult {
                    Data = "Success"
                });
            }
            catch (Exception e)
            {
                message.Dispose();
            }
            return(new JsonResult {
                Data = emailSuccessful
            });
        }