public static ClientResponse InsertGestionCorreo(GestionCorreo objeto)
        {
            ClientResponse response;

            try
            {
                using (GestionCorreoDAO dbGestionCorreo = new GestionCorreoDAO())
                {
                    response = dbGestionCorreo.InsertGestionCorreo(objeto);
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return(response);
        }
        public static ClientResponse ImportarCorreos()
        {
            ClientResponse response;

            try
            {
                using (GestionCorreoDAO dbGestionCorreo = new GestionCorreoDAO())
                {
                    response = dbGestionCorreo.ImportarCorreos();
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return(response);
        }
        public static ClientResponse getListGestionCorreo(int paginaActual, int RegistroXpagina, int id_cbo_origenfil, int id_cbo_grupo_consultar)
        {
            ClientResponse response;

            try
            {
                using (GestionCorreoDAO dbGestionCorreo = new GestionCorreoDAO())
                {
                    response = dbGestionCorreo.getLstGestionCorreo(paginaActual, RegistroXpagina, id_cbo_origenfil, id_cbo_grupo_consultar);
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return(response);
        }
        public static ClientResponse dardeBaja()
        {
            ClientResponse response;

            try
            {
                GestionCorreo objeto = new GestionCorreo()
                {
                    Tokens = tokens
                };

                using (GestionCorreoDAO dbGestionCorreo = new GestionCorreoDAO())
                {
                    response = dbGestionCorreo.DarDebajaEnviocorreos(objeto);
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return(response);
        }
        public static ClientResponse getExportarExcel(int id_cbo_origenfil, int id_cbo_grupo_consultar)
        {
            ClientResponse response;

            try
            {
                string         hora = DateTime.Now.ToString("yyyyMMddhhmmss");
                ClientResponse responserutaexcel;
                using (ParametrosMaestrosDAO dbParametrosMaestro = new ParametrosMaestrosDAO())
                {
                    responserutaexcel = dbParametrosMaestro.getObjParametroMaestro("RUTA_DESCARGA_EXCEL");
                }
                ParametrosMaestros rutaexcel = Newtonsoft.Json.JsonConvert.DeserializeObject <ParametrosMaestros>(responserutaexcel.DataJson);


                ClientResponse responserutaexcelsite;
                using (ParametrosMaestrosDAO dbParametrosMaestro = new ParametrosMaestrosDAO())
                {
                    responserutaexcelsite = dbParametrosMaestro.getObjParametroMaestro("RUTA_SITE_DESCARGA_EXCEL");
                }
                ParametrosMaestros rutaexcelsite = Newtonsoft.Json.JsonConvert.DeserializeObject <ParametrosMaestros>(responserutaexcelsite.DataJson);

                if (Directory.Exists(rutaexcel.valor))
                {
                    Directory.Delete(rutaexcel.valor, true);
                }

                string savepath = rutaexcel.valor + hora;

                using (GestionCorreoDAO dbGestionCorreo = new GestionCorreoDAO())
                {
                    response = dbGestionCorreo.getExportarCorreos(id_cbo_origenfil, id_cbo_grupo_consultar);
                }
                List <GestionCorreo> listCorreos = Newtonsoft.Json.JsonConvert.DeserializeObject <List <GestionCorreo> >(response.DataJson);

                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                // Create empty workbook
                excel.Workbooks.Add();
                // Create Worksheet from active sheet
                Microsoft.Office.Interop.Excel._Worksheet workSheet = excel.ActiveSheet;
                try
                {
                    // ------------------------------------------------
                    // Creation of header cells
                    // ------------------------------------------------
                    // ------------------------------------------------
                    // Creation of header cells
                    // ------------------------------------------------
                    workSheet.Cells[1, "A"] = "Grupo";
                    workSheet.Cells[1, "B"] = "Nombre 1";
                    workSheet.Cells[1, "C"] = "Nombre 2";
                    workSheet.Cells[1, "D"] = "Paterno";
                    workSheet.Cells[1, "E"] = "Materno";
                    workSheet.Cells[1, "F"] = "Email";
                    workSheet.Cells[1, "G"] = "Estado";
                    workSheet.Cells[1, "H"] = "Fecha Baja";
                    // ------------------------------------------------
                    // Populate sheet with some real data from "cars" list
                    // ------------------------------------------------
                    int row = 2; // start row (in row 1 are header cells)
                    foreach (GestionCorreo car in listCorreos)
                    {
                        workSheet.Cells[row, "A"] = car.grupocorreo.descripcion;
                        workSheet.Cells[row, "B"] = car.Nombre1;
                        workSheet.Cells[row, "C"] = car.Nombre2;
                        workSheet.Cells[row, "D"] = car.ApePaterno;
                        workSheet.Cells[row, "E"] = car.ApeMaterno;
                        workSheet.Cells[row, "F"] = car.Email;
                        workSheet.Cells[row, "G"] = car.descestado;
                        workSheet.Cells[row, "H"] = car.fechabaja;
                        row++;
                    }
                    // Apply some predefined styles for data to look nicely :)
                    workSheet.Range["A1"].AutoFormat(Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic1);
                    if (!Directory.Exists(savepath))
                    {
                        Directory.CreateDirectory(savepath);
                    }
                    // Define filename
                    string fileNamea = string.Format(@"{0}\ExcelData.xlsx", Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
                    string fileName  = savepath + "\\" + "ExcelData.xlsx";
                    // Save this data as a file
                    workSheet.SaveAs(fileName);
                    // Display SUCCESS messager
                    response.Mensaje  = "Ok";
                    response.DataJson = rutaexcelsite.valor + hora + "/" + "ExcelData.xlsx";
                }
                catch (Exception exception)
                {
                    response.Mensaje  = "Error";
                    response.DataJson = exception.Message;
                }
                finally
                {
                    // Quit Excel application
                    excel.Quit();

                    // Release COM objects (very important!)
                    if (excel != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
                    }

                    if (workSheet != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
                    }

                    // Empty variables
                    excel     = null;
                    workSheet = null;

                    // Force garbage collector cleaning
                    GC.Collect();
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return(response);
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Expires     = -1;
            try

            {
                string hora          = DateTime.Now.ToString("yyyyMMddhhmmss");
                string tempPath      = "";
                string savepath      = "";
                int    cbo_origen    = 0;
                int    cbo_plantilla = 0;
                int.TryParse(context.Request["cbo_origen"], out cbo_origen);
                int.TryParse(context.Request["cbo_plantilla"], out cbo_plantilla);
                string             txt_prompt      = context.Request["txt_prompt"];
                string             txt_asunto      = context.Request["txt_asunto"];
                string             id_grupo_correo = context.Request.Form.Get("list");
                string             usuarioSession  = context.Request.Form.Get("txt_UserSession");
                HttpFileCollection uploadFiles     = context.Request.Files;
                List <string>      listrutas       = new List <string>();
                List <LogPromoDet> lstLogPromoDet  = new List <LogPromoDet>();

                ClientResponse responseruta;
                using (ParametrosMaestrosDAO dbParametrosMaestro = new ParametrosMaestrosDAO())
                {
                    responseruta = dbParametrosMaestro.getObjParametroMaestro("RUTA_ADJUNTO_ENVIO_CORREOS");
                }
                ParametrosMaestros rutaadjunto = Newtonsoft.Json.JsonConvert.DeserializeObject <ParametrosMaestros>(responseruta.DataJson);



                for (int i = 0; i < uploadFiles.Count; i++)
                {
                    HttpPostedFile postedFile = uploadFiles[i];
                    //tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"] + rutaadjunto.valor + hora;
                    tempPath = rutaadjunto.valor + hora;

                    savepath = tempPath;
                    if (!Directory.Exists(savepath))
                    {
                        Directory.CreateDirectory(savepath);
                    }

                    string filename = postedFile.FileName;
                    string files    = savepath + @"\" + filename;
                    postedFile.SaveAs(files);
                    listrutas.Add(files);
                }

                ClientResponse response;
                string         usuario = cUtil.ObtenerValorParametro("CORREOPROMO", "USER");
                string         clave   = cUtil.ObtenerValorParametro("CORREOPROMO", "CLAVE");
                string         smtp    = cUtil.ObtenerValorParametro("CORREOPROMO", "SMTP");
                int            puerto  = Convert.ToInt32(cUtil.ObtenerValorParametro("CORREOPROMO", "PUERTO"));

                ClientResponse responsedesafiliacion;
                using (ParametrosMaestrosDAO dbParametrosMaestro = new ParametrosMaestrosDAO())
                {
                    responsedesafiliacion = dbParametrosMaestro.getObjParametroMaestro("DESAFILACIONPROMOCIONES");
                }
                ParametrosMaestros desafiliacion = Newtonsoft.Json.JsonConvert.DeserializeObject <ParametrosMaestros>(responsedesafiliacion.DataJson);



                ClientResponse responseplantilla;
                using (PlantillaDAO dbPlanilla = new PlantillaDAO())
                {
                    responseplantilla = dbPlanilla.getPlantillaXId(cbo_plantilla);
                }
                Plantilla objetoplantilla = Newtonsoft.Json.JsonConvert.DeserializeObject <Plantilla>(responseplantilla.DataJson);

                string body = string.Empty;
                using (StreamReader reader = new StreamReader(objetoplantilla.ruta_plantilla_html))
                {
                    body = reader.ReadToEnd();
                }
                body = "<div style='padding: 0px 2px; '>" + body + "</div>";
                using (GestionCorreoDAO dbGestionCorreoDAO = new GestionCorreoDAO())
                {
                    response = dbGestionCorreoDAO.getLstGestionCorreoXGrupo(id_grupo_correo);
                }
                List <GestionCorreo> liscorreos = Newtonsoft.Json.JsonConvert.DeserializeObject <List <GestionCorreo> >(response.DataJson);
                string bodyaux = string.Empty;

                LogPromo oLogPromo = new LogPromo();
                oLogPromo.remitente   = usuarioSession;
                oLogPromo.asunto      = txt_asunto;
                oLogPromo.PlantillaID = objetoplantilla.id;

                foreach (GestionCorreo item in liscorreos)
                {
                    bodyaux = body;
                    if (objetoplantilla.fl_parrafo == 1)
                    {
                        bodyaux = bodyaux.Replace("{parrafo}", txt_prompt);
                    }
                    bodyaux = bodyaux.Replace("{NombreUsuario}", item.Nombre1 + " " + item.Nombre2);
                    bodyaux = bodyaux.Replace("{Codigo}", item.codigo.Trim());
                    bodyaux = bodyaux.Replace("{linkdardebaja}", desafiliacion.valor + "?tokens=" + item.Tokens);
                    bodyaux = HttpUtility.HtmlDecode(bodyaux);
                    cUtil.EnvioMailSegundo(txt_asunto, item.Email, bodyaux, listrutas, usuario, clave, smtp, puerto);

                    LogPromoDet oLogPromoDet = new LogPromoDet();
                    oLogPromoDet.id_grupo_correo = item.grupocorreo.id;// Convert.ToInt32(id_grupo_correo);
                    oLogPromoDet.destinatario    = item.Email.ToString();
                    lstLogPromoDet.Add(oLogPromoDet);
                }
                oLogPromo.LogPromoDet = lstLogPromoDet;

                ClientResponse responseLogPromo;
                using (LogPromoDAO dbLogPromo = new LogPromoDAO())
                {
                    responseLogPromo = dbLogPromo.InsertLogPromo(oLogPromo);
                }

                //Directory.Delete(savepath, true);

                //plantilla.list_plantilla_detalle = list_plantilla_detalle;
                //ClientResponse response;
                //using (PlantillaDAO dbPlanilla = new PlantillaDAO())
                //{
                //    response = dbPlanilla.InsertPantilla(plantilla);
                //}

                //if ((System.IO.File.Exists(files)))
                //{
                //    System.IO.File.Delete(files);
                //}

                var result = new
                {
                    Result  = "Ok",
                    Mensaje = "Se envio correo"
                };
                context.Response.Write(JsonConvert.SerializeObject(result));
            }

            catch (Exception ex)

            {
                var result = new
                {
                    Result  = "Error",
                    Mensaje = ex.Message
                };
                context.Response.Write(JsonConvert.SerializeObject(result));
            }
        }
Ejemplo n.º 7
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Expires     = -1;

            try
            {
                string         hora = DateTime.Now.ToString("yyyyMMddhhmmss");
                ClientResponse responseruta;
                using (ParametrosMaestrosDAO dbParametrosMaestro = new ParametrosMaestrosDAO())
                {
                    responseruta = dbParametrosMaestro.getObjParametroMaestro("RUTA_CARGAEXCELCORREOS");
                }
                ParametrosMaestros rutaexcel  = Newtonsoft.Json.JsonConvert.DeserializeObject <ParametrosMaestros>(responseruta.DataJson);
                HttpPostedFile     postedFile = context.Request.Files["Filedata"];
                listgestioncorreo = new List <GestionCorreo>();

                string savepath = "";
                string tempPath = "";

                tempPath = rutaexcel.valor + hora;
                savepath = tempPath;
                string filename = postedFile.FileName;

                if (!Directory.Exists(savepath))
                {
                    Directory.CreateDirectory(savepath);
                }
                string files = savepath + "/" + filename;
                postedFile.SaveAs(files);

                Excel.Application xlApp;
                Excel.Workbook    xlWorkBook;
                Excel.Worksheet   xlWorkSheet;
                Excel.Range       range;

                string str;
                int    rCnt;
                int    cCnt;
                int    rw = 0;
                int    cl = 0;

                xlApp       = new Excel.Application();
                xlWorkBook  = xlApp.Workbooks.Open(files, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                range = xlWorkSheet.UsedRange;
                rw    = range.Rows.Count;
                cl    = 6;// range.Columns.Count; Leemos solo 6 columnas de cada fila

                ClientResponse response;
                using (GrupoCorreoDAO dbGrupoCorreo = new GrupoCorreoDAO())
                {
                    response = dbGrupoCorreo.getGrupoCorreoCombo(2);
                }

                List <GrupoCorreo> list = JsonConvert.DeserializeObject <List <GrupoCorreo> >(response.DataJson);

                for (rCnt = 1; rCnt <= rw; rCnt++)
                {
                    if (rCnt > 1)
                    {
                        entidad = new GestionCorreo();
                        for (cCnt = 1; cCnt <= cl; cCnt++)
                        {
                            if (cCnt == 1)
                            {
                                GrupoCorreo grupocorreo = new GrupoCorreo();
                                grupocorreo.descripcion = str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                                entidad.grupocorreo     = grupocorreo;
                                //Validamos si el grupo de correo esta registrado en la base de datos
                                grupocorreo.origen = 2;//Carga de Correo externo
                                grupocorreo.estado = 1;
                                GrupoCorreo objeto = list.Where(i => i.descripcion.ToUpper() == grupocorreo.descripcion.ToUpper()).FirstOrDefault();
                                if (objeto != null)
                                {
                                    grupocorreo.id = objeto.id;
                                }
                            }
                            if (cCnt == 2)
                            {
                                entidad.Nombre1 = str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                            }
                            if (cCnt == 3)
                            {
                                entidad.Nombre2 = str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                            }
                            if (cCnt == 4)
                            {
                                entidad.ApePaterno = str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                            }
                            if (cCnt == 5)
                            {
                                entidad.ApeMaterno = str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                            }
                            if (cCnt == 6)
                            {
                                entidad.Email = str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                            }
                        }
                        entidad.id_estado = 1;
                        listgestioncorreo.Add(entidad);
                    }
                }

                xlWorkBook.Close(true, null, null);
                xlApp.Quit();

                Marshal.ReleaseComObject(xlWorkSheet);
                Marshal.ReleaseComObject(xlWorkBook);
                Marshal.ReleaseComObject(xlApp);



                if ((System.IO.File.Exists(files)))
                {
                    System.IO.File.Delete(files);
                }

                if (listgestioncorreo.Count() > 0)
                {
                    ClientResponse response1;
                    using (GestionCorreoDAO dbGestionCorreo = new GestionCorreoDAO())
                    {
                        response1 = dbGestionCorreo.InsertGestionCorreoAutomatico(listgestioncorreo);
                    }
                }


                var wapper = new
                {
                    Result  = "Ok",
                    Mensaje = "Se cargaron correctaente la trama."
                };
                context.Response.Write(JsonConvert.SerializeObject(wapper));
            }

            catch (Exception ex)

            {
                var wapper = new
                {
                    Result  = "Error",
                    Mensaje = ex.Message
                };
                context.Response.Write(JsonConvert.SerializeObject(wapper));
            }
        }