public bool Guardar(LARCA20_Emails email) { return(EmailsDAL.Guardar(email)); }
/// <summary> /// Metodo que se encarga de leer todos los emails de la casilla suministrada por P&G, levantara solo los que pertenezcan a LARCA /// </summary> /// <returns></returns> public List <MasterRow> ProcesarEmails() { var list = new List <MasterRow>(); const string subject = "XLS SAMPLE"; const int port = 110; var attachments = new ApplicationDataBLL().TraerCantidadAdjuntos(); var emailsService = new EmailsBLL(); using (var client = new Pop3Client()) { try { // Conexion al servidor de emails client.Connect(ServerPop, port, false); client.Authenticate(UserName, Password); int messageCount = client.GetMessageCount(); int tope = messageCount - 1000; if (tope < 0) { tope = 0; } for (int i = messageCount; i > tope; i--) { try { Message message = client.GetMessage(i); // Si el asunto es EL indicado (Capitalizando todo) if (message.Headers.Subject.ToUpper() == subject.ToUpper()) { // Si el email no se encuentra ya procesado en nuestra base if (!emailsService.EmailExiste(message.Headers.MessageId)) { // Recorremos los adjuntos y los descargamos (en caso que sea la cantidad indicada) var adjuntos = message.FindAllAttachments(); if (adjuntos.Count == attachments) { foreach (var adjunto in adjuntos) { // Si el adjunto contiene el MIME tipo Excel if (GetXmlMimes().Exists(s => s == adjunto.ContentType.MediaType)) { // Generamos el archivo fisico y lo guardamos en un directorio var date = DateTime.Now.ToString("ddMMMyyy"); var random = new Random().Next(0, 1000); string fileName = String.Format("{0}_MasterData_{1}.xlsx", date, random); string filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data/"), fileName); var stream = new FileStream(filePath, FileMode.Create); var binaryStream = new BinaryWriter(stream); binaryStream.Write(adjunto.Body); stream.Close(); binaryStream.Close(); // Detectamos que tipo de proceso hay que aplicarle al archivo en base a su dia de la semana var email = new LARCA20_Emails(); if (DateTime.Now.DayOfWeek == DayOfWeek.Tuesday) { email.processtype = (int)TipoProceso.Parcial; } else { email.processtype = (int)TipoProceso.Total; } // Completamos la info del email para guardar en la base email.MessageID = message.Headers.MessageId; email.date = message.Headers.DateSent; email.deleted = false; email.filename = fileName; // Enviamos a procesar el Excel descargado del email try { list = new ExcelCore().ProcesarExcel(email.filename, (TipoProceso)email.processtype); email.processed = true; emailsService.Guardar(email); } catch { list = new List <MasterRow>(); email.processed = false; emailsService.Guardar(email); } break; } } } else { EnviarAlertaAdjuntos_Admin(message); // Enviar email que no esta la cantidad de adjuntos que corresponde } } } } catch (Exception ex) { // Logggear Excepcion } } } catch (Exception ex) { // Logggear Excepcion } return(list); } }