Exemplo n.º 1
0
        public static string CompletaParticipante(string nombre, string apellido, string email, string ciudad, string direccion,
                                                  string CP, string DNI)
        {
            var          fromAddress  = new MailAddress("*****@*****.**", "TPWEB");
            var          toAddress    = new MailAddress(email, nombre + " " + apellido);
            const string fromPassword = "******";
            const string subject      = "Sorteo !";
            const string body         = "Has completado todo y ganado el premio. Pronto te lo llevaran a tu domicilio";

            var smtp = new SmtpClient
            {
                Host                  = "smtp.gmail.com",
                Port                  = 587,
                EnableSsl             = true,
                DeliveryMethod        = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials           = new NetworkCredential(fromAddress.Address, fromPassword)
            };
            Participante nuevo = new Participante();

            nuevo.nombre    = nombre;
            nuevo.apellido  = apellido;
            nuevo.email     = email;
            nuevo.ciudad    = ciudad;
            nuevo.direccion = direccion;
            nuevo.CP        = CP;
            nuevo.DNI       = DNI;
            if (Datos.validacion(nuevo).Count != 0)
            {
                string error = "No se puede realizar la operacion porque se encontaron error en los siguiente campos: ";
                foreach (var item in Datos.validacion(nuevo))
                {
                    error += " " + item + ",";
                }
                error = error.Remove(error.Length - 1);
                return("{ \"Error\" :1 ,\"descripcion\": \"" + error + "\"}");
            }
            if (!(bool)HttpContext.Current.Session["EsCliente"])
            {
                if (!PersonaNegocio.InsertarPersona(nuevo))
                {
                    return("{ \"Error\" :1 ,\"descripcion\": \"No se pudo cuardar los datos reinten de nuevo\"}");
                }
            }


            if (VoucherNegocio.CambiarEstado(HttpContext.Current.Session["voucher"].ToString()))
            {
                if (PremioNegocio.asignar(nuevo.DNI, HttpContext.Current.Session["voucher"].ToString(), HttpContext.Current.Session["idPremio"].ToString()))
                {
                    HttpContext.Current.Session["voucher"]  = null;
                    HttpContext.Current.Session["idPremio"] = null;
                    HttpContext.Current.Session["gano"]     = true;
                    using (var message = new MailMessage(fromAddress, toAddress)
                    {
                        Subject = subject,
                        Body = body
                    })
                    {
                        smtp.Send(message);
                    }
                    return("{ \"Error\" :0 }");
                }
                else
                {
                    return("{ \"Error\" :1,\"descripcion\": \"No se puedo enviar el correo\"}");
                }
            }
            else
            {
                return("{ \"Error\" :1 ,\"descripcion\": \"Error en uso de voucher intente nuevamente\"}");
            }
        }