Пример #1
0
        public static IRestResponse SendMailMailgun(MailMessage mail)
        {
            string apiKey         = Parametro.GetValue("MailGunApiKey");
            string mailGunDominio = Parametro.GetValue("MailGunDominio");

            try
            {
                RestClient client = new RestClient();
                client.BaseUrl       = new Uri(Parametro.GetValue("UrlApiMailgun"));
                client.Authenticator = new HttpBasicAuthenticator("api", apiKey);
                RestRequest request = new RestRequest();
                request.AddParameter("domain", mailGunDominio, ParameterType.UrlSegment);
                request.Resource = "{domain}/messages";
                request.AddParameter("from", "Qualia <" + mail.From.Address + ">");
                request.AddParameter("to", mail.To.ToString());
                request.AddParameter("subject", mail.Subject);
                request.AddParameter("html", mail.Body);
                request.Method = Method.POST;
                var send = client.Execute(request);
                return(send);
            }
            catch
            {
                throw;
            }
        }
Пример #2
0
        private static void SendMailSMTP(MailMessage mail)
        {
            try
            {
                //Obtengo valores de tabla parámetro:

                string servidor  = Parametro.GetValue("Servidor");
                bool   enableSsl = Parametro.GetValue("EnableSsl") == "1" ? true : false;
                int    port      = Convert.ToInt32(Parametro.GetValue("Puerto"));
                string usr       = Parametro.GetValue("Usuario");
                string psw       = Parametro.GetValue("Contraseña");

                SmtpClient cliente = new SmtpClient(servidor)
                {
                    EnableSsl   = enableSsl,
                    Port        = port,
                    Credentials = new System.Net.NetworkCredential(usr, psw)
                };
                cliente.Send(mail);
            }
            catch
            {
                throw;
            }
        }
Пример #3
0
        private MailMessage ArmadoMailForgotPassword(MailUsuarioModel usuario)
        {
            string urlChangePass = urlBackend + "Account/ChangePassByMail?token=" + usuario.TokenUrl;
            string body          = File.ReadAllText(path + "/resetForgotPass.html");
            string subject       = "Solicitud para restablecer contraseña olvidada";
            string from          = Parametro.GetValue("Remitente");
            string fromName      = Parametro.GetValue("RemitenteNombre");

            body = body.Replace("[@url-change-pass]", urlChangePass);
            body = body.Replace("[@nombre-usuario]", usuario.UsuarioNombre + " " + usuario.UsuarioApellido);

            return(CreateMailMessage(from, fromName, subject, body, usuario.Email));
        }
Пример #4
0
        public MailMessage ArmadoMailNewUsuario(MailUsuarioModel usuario)
        {
            string      subjectAlta     = String.Empty;
            string      urlChangePass   = urlBackend + "Account/ChangePassByMail?token=" + usuario.TokenUrl;
            string      body            = File.ReadAllText(path + "/nuevoUsuario.html");
            string      from            = Parametro.GetValue("Remitente");
            string      fromName        = Parametro.GetValue("RemitenteNombre");
            string      contentUserType = String.Empty;
            MailMessage mail;

            if (usuario.EsAdministrador)
            {
                subjectAlta     = "Tu usuario administrador ya esta disponible!";
                contentUserType = "<li>Usuario-Admin: <b>" + usuario.Usuario + "</b></li>";
                body            = body.Replace("[@base-url]", urlBackend);
                body            = body.Replace("[@url-change-pass]", urlChangePass);
                body            = body.Replace("[@password]", usuario.Password);
                body            = body.Replace("[@nombre-usuario]", usuario.UsuarioNombre + " " + usuario.UsuarioApellido);
                body            = body.Replace("[@seccion-user-type]", contentUserType);

                mail = new MailMessage
                {
                    IsBodyHtml = true,
                    Body       = body,
                    Subject    = subjectAlta,
                    From       = new MailAddress(from, fromName),
                };
                mail.To.Add(usuario.Email);
                return(mail);
            }

            subjectAlta     = "Tu usuario ya esta disponible!";
            contentUserType = "<li>Usuario: <b>" + usuario.Usuario + "</b></li>";
            body            = body.Replace("[@base-url]", urlBackend);
            body            = body.Replace("[@url-change-pass]", urlChangePass);
            body            = body.Replace("[@password]", usuario.Password);
            body            = body.Replace("[@nombre-usuario]", usuario.UsuarioNombre + " " + usuario.UsuarioApellido);
            body            = body.Replace("[@seccion-user-type]", contentUserType);

            mail = new MailMessage
            {
                IsBodyHtml = true,
                Body       = body,
                Subject    = subjectAlta,
                From       = new MailAddress(from, fromName)
            };
            mail.To.Add(usuario.Email);
            return(mail);
        }
Пример #5
0
        /* ENVIO DE EMAILS */
        public static void SendMail(MailMessage mail)
        {
            string metodoEnvio = Parametro.GetValue("MetodoEnvio");

            switch (metodoEnvio)
            {
            case "SMTP":
                SendMailSMTP(mail);
                break;

            case "Mailgun":
                SendMailMailgun(mail);
                break;
            }
        }
Пример #6
0
        public string CreateUsuario(UsuarioViewModel model)
        {
            var rolesList = model.ArrayRoles != null?model.ArrayRoles.Split(',').ToList() : new List <string>();

            if (ValidarUsuario(model))
            {
                try
                {
                    Random r                  = new Random();
                    int    randNum            = r.Next(1000000);
                    string sixDigitNumber     = randNum.ToString("D6");
                    var    encripted_password = Encrypter.Encryption(sixDigitNumber, Parametro.GetValue("3DESKey"));
                    _logger.LogInformation("Contraseña encriptada correctamente.");
                    var usuario = new Usuario()
                    {
                        Apellido   = model.Apellido,
                        Nombre     = model.Nombre,
                        Email      = model.Email,
                        Contrasena = encripted_password
                    };

                    var result = _uow.UsuarioRepository.Create(usuario);
                    _uow.UsuarioRepository.Save();

                    foreach (var rol in rolesList)
                    {
                        var usuarioRol = new UsuarioRol()
                        {
                            RolId     = int.Parse(rol),
                            UsuarioId = result.UsuarioId
                        };
                        _uow.UsuarioRolRepository.Create(usuarioRol);
                        _uow.UsuarioRolRepository.Save();
                    }
                    var tokenGuid = Guid.NewGuid().ToString();
                    var token     = new UsuarioToken()
                    {
                        UsuarioId       = result.UsuarioId,
                        FechaExpiracion = DateTime.Now.AddMinutes(Convert.ToDouble(Helper.Parametro.GetValue("TiempoExpiracionTokenMail"))),
                        Usado           = false,
                        Token           = tokenGuid
                    };
                    MailUsuarioModel mailModel = new MailUsuarioModel()
                    {
                        TokenUrl        = tokenGuid,
                        Password        = sixDigitNumber,
                        UsuarioApellido = result.Apellido,
                        UsuarioNombre   = result.Nombre,
                        Email           = result.Email,
                        Usuario         = result.Email,
                        EsAdministrador = _uow.UsuarioRolRepository.AllIncluding(x => x.Rol).Any(x => x.UsuarioId == result.UsuarioId && x.Rol.Codigo == Helper.EstadosHelper.UsuarioDefault.ADM.ToString())
                    };

                    sender.SendMailNewUsuario(mailModel);
                    var tokenCreated = _uow.UsuarioTokenRepository.Create(token);
                    _uow.UsuarioRepository.Save();
                    _logger.LogInformation("Envio de email correctamente");
                    _logger.LogInformation("Usuario creado correctamente. Nombre de usuario: <{0}> - Roles asignados: <{1}>.", model.Nombre + " " + model.Apellido, rolesList);
                    return("Usuario creado correctamente.");
                }
                catch (Exception ex)
                {
                    _logger.LogErrorException(ex,
                                              "Ocurrió un error al crear usuario. Nombre de usuario: <{0}> - Roles asignados: <{1}>.",
                                              model.Nombre + " " + model.Apellido, rolesList.ToString());
                    throw new CreateRecordException("Ocurrió un error al crear el usuario", ex);
                }
            }
            else
            {
                _logger.LogError(
                    "[ERROR VALIDACION EN SERVER] - Ocurrió un error al crear usuario. Uno o mas datos son incorrectos.");
                return("FailModel");
            }
        }