예제 #1
0
        private static bool EnviarEmail(Pericias _pericia)
        {
            List <CuentaEmailPorUsuario> cuenta = new List <CuentaEmailPorUsuario>();

            cuenta = UsuarioDao.BuscarCuentaEmailPorUsuario(_pericia.UsuarioLogin);
            var       Cuenta     = cuenta.First();
            Variables _variables = new Variables();
            bool      exito      = false;
            string    emisor     = Cuenta.CuentaEmail;
            string    pwd        = Cuenta.ClaveEmail;
            string    correo     = "";

            if (_pericia.TotalEstados > 1)
            {
                correo = "Estimada/o, le informamos que se creo un nuevo movimiento en la pericia con Número de causa " + _pericia.NroCausa + ", referente a la causa " + _pericia.Causa + " <br />abierta en el " + _pericia.Tribunal + " con fecha de creación " + _pericia.Fecha + ". <br /> Se informa que lo siguiente respecto a la pericia: " + _pericia.Descripcion + "<br />" + Cuenta.FirmaEmail + "<br />.";
            }
            else
            {
                correo = "Estimada/o, le informamos que se inicio una nueva pericia con Número de causa " + _pericia.NroCausa + ", referente a la causa " + _pericia.Causa + " <br /> abierta en el " + _pericia.Tribunal + " con fecha de creación " + _pericia.Fecha + ". <br /> " + Cuenta.FirmaEmail + ".";
            }
            List <string> adjuntos = new List <string>();
            string        adjunto1 = Adj1;

            if (adjunto1 != null)
            {
                adjuntos.Add(adjunto1);
            }
            string adjunto2 = Adj2;

            if (adjunto2 != null)
            {
                adjuntos.Add(adjunto2);
            }
            string adjunto3 = Adj3;

            if (adjunto3 != null)
            {
                adjuntos.Add(adjunto3);
            }
            string adjunto4 = Adj4;

            if (adjunto4 != null)
            {
                adjuntos.Add(adjunto4);
            }
            string adjunto5 = Adj5;

            if (adjunto5 != null)
            {
                adjuntos.Add(adjunto5);
            }
            string adjunto6 = Adj6;

            if (adjunto6 != null)
            {
                adjuntos.Add(adjunto6);
            }
            string adjunto7 = Adj7;

            if (adjunto7 != null)
            {
                adjuntos.Add(adjunto7);
            }
            string adjunto8 = Adj8;

            if (adjunto8 != null)
            {
                adjuntos.Add(adjunto8);
            }
            string adjunto9 = Adj9;

            if (adjunto9 != null)
            {
                adjuntos.Add(adjunto9);
            }
            string adjunto10 = Adj10;

            if (adjunto10 != null)
            {
                adjuntos.Add(adjunto10);
            }

            //if (adjuntos.Count > 1)
            //{ ComprimirArchivos(adjuntos); }

            MailMessage msg = new MailMessage();

            //Quien escribe al correo
            msg.From = new MailAddress(emisor);
            //A quien va dirigido
            msg.To.Add(new MailAddress(_pericia.Email));
            //Asunto
            msg.Subject = "Pericia " + _pericia.NroCausa + " por la causa " + _pericia.Causa + "";
            //Contenido del correo
            msg.Body       = correo;
            msg.IsBodyHtml = true;
            //Adjuntamos archivo
            //string tempFileName = Oid.ToString();

            foreach (string attach in adjuntos)
            {
                Attachment attached = new Attachment(attach, System.Net.Mime.MediaTypeNames.Application.Octet);
                msg.Attachments.Add(attached);
            }
            SmtpClient client = new SmtpClient();

            client.Credentials = new System.Net.NetworkCredential(emisor, pwd);
            client.Port        = 587;
            client.Host        = "smtp.gmail.com";
            client.EnableSsl   = true; //Esto es para que vaya a través de SSL que es obligatorio con GMail
            try
            {
                client.Send(msg);
                exito = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Atención si bien la pericia se guardo correctamente, Fallo el envio de email.");
            }
            return(exito);
        }