public async Task <ResponseModel> RetrievePassword(RecoveryPassword model) { ResponseModel response = new ResponseModel(); ValidationResult validateRecoveryEmail = new RecoveryEmailValidator().Validate(model); if (validateRecoveryEmail.IsValid) { try { User user = _userRepository.GetByEmail(model.Email); if (user == null) { response.Message = "Usuário não encontrado."; return(response); } EmailRetrievePassword emailRetrieve = new EmailRetrievePassword() { FromEmail = "*****@*****.**", FromName = "Me Agenda Aí", Subject = "Link para alteração da sua senha do Me Agenda Aí", Token = JWTService.GenerateTokenRecoverPassword(user, _signingConfiguration, _tokenConfiguration), Url = _configuration.GetValue <string>("URLPortal"), Expiration = ((Convert.ToInt32(_tokenConfiguration.Seconds) / (60 * 60)).ToString()) }; bool resp = await _email.SendRecoveryPassword(user, emailRetrieve); if (resp) { response.Success = resp; response.Message = "E-mail enviado com sucesso."; } else { response.Message = "Não foi possível enviar o e-mail."; } } catch (Exception) { response.Message = "Erro no sistema, e-mail não enviado."; } } else { response.Message = validateRecoveryEmail.Errors.FirstOrDefault().ErrorMessage; } return(response); }
public async Task <bool> SendRecoveryPassword(Domain.Entities.User user, EmailRetrievePassword emailRetrieve) { try { MailjetClient client = newMailjetClient(APIKEY, APISECRET); MailjetRequest request = new MailjetRequest { Resource = Send.Resource } .Property(Send.FromEmail, emailRetrieve.FromEmail) .Property(Send.FromName, emailRetrieve.FromName) .Property(Send.Subject, emailRetrieve.Subject) .Property(Send.Recipients, new JArray { new JObject { { "Email", user.Email }, { "Name", user.Name } } }) .Property(Send.MjTemplateID, 2267909) .Property(Send.MjTemplateLanguage, "True") .Property(Send.Vars, new JObject { { "user_name", user.Name }, { "link_reset", GenerateURL(emailRetrieve.Url + "/redefinir-senha", user.UserId.ToString(), emailRetrieve.Token) }, { "expiration", emailRetrieve.Expiration } }); MailjetResponse resp = await client.PostAsync(request); if (resp.IsSuccessStatusCode) { return(true); } else { return(false); } } catch (Exception e) { throw e; } }