Exemple #1
0
        public ActionResult EmailVisita(Int32 VisitaCorretajeId)
        {
            try
            {
                VisitaCorretaje visita = context.VisitaCorretaje.FirstOrDefault(x => x.VisitaCorretajeId == VisitaCorretajeId);

                var        usuario   = context.Usuario.FirstOrDefault(x => x.UsuarioId == 1531);
                EmailLogic mailLogic = new EmailLogic();
                ViewModel.Templates.infoViewModel model = new ViewModel.Templates.infoViewModel();
                model.Mensaje = "Estimado Usuario\nSe adjunta la constancia de visita realizada.";
                model.Firma   = usuario.Firma;

                if (!String.IsNullOrEmpty(visita.Correo))
                {
                    mailLogic.SendEmailMasivoVisita("Constancia de Visita " + visita.Fecha.ToString("dd/MM/yyyy"), "info", usuario.Email
                                                    , usuario.NombreRemitente, visita.Correo, model, null
                                                    , null,
                                                    visita);
                }
                PostMessage(MessageType.Success);
            }
            catch (Exception ex)
            {
                PostMessage(MessageType.Error, ex.Message + (ex.InnerException != null ? ex.InnerException.Message : String.Empty));
            }

            return(RedirectToAction("LstVisitas"));
        }
Exemple #2
0
        public ActionResult _ForgotPassword(_ForgotPasswordViewModel model)
        {
            try
            {
                EmailLogic mailLogic = new EmailLogic(this, CargarDatosContext());
                ViewModel.Templates.infoViewModel mailModel = new ViewModel.Templates.infoViewModel();

                var Lstusuario = context.Usuario.Where(x => x.Email == model.Email).ToList();
                if (Lstusuario.Count == 0)
                {
                    PostMessage(MessageType.Error, "El correo " + model.Email + " no se encuentra registrado en el sistema. Intente nuevamente ingresando un correo registrado en el sistema.");
                    return(RedirectToAction("Login", "Home"));
                }
                var Mensaje = String.Empty;
                foreach (var item in Lstusuario)
                {
                    if (item.Departamento != null)
                    {
                        Mensaje += "Edificio: " + item.Departamento.Edificio.Nombre + " - Departamento: " + item.Departamento.Numero + "<br/>";
                        Mensaje += "Usuario: " + item.Codigo + "<br/>";
                        Mensaje += "Contraseña: " + item.Password + "<br/>";
                        Mensaje += "---------------------------------<br/>";
                    }
                }
                mailModel.Mensaje = Mensaje;
                mailModel.Titulo  = "Recuperar Contraseña";

                mailLogic.SendEmail("Recuperar Contraseña", "ForgorPassword", "*****@*****.**", "Afari", model.Email, mailModel, null);
                PostMessage(MessageType.Success, "Se envió un email con los datos de acceso a " + model.Email);
                return(RedirectToAction("Login", "Home"));
            }
            catch (Exception ex)
            {
                PostMessage(MessageType.Error, "No se pudo enviar email.");
                return(RedirectToAction("Login", "Home"));
            }
        }
Exemple #3
0
        public string SendEmailMasivoVisita(string asunto, string plantilla, string remitente, string nombreRemitente, string receptor, ViewModel.Templates.infoViewModel model = null, String copiacarbon = null, String copiaOculta = null, VisitaCorretaje visita = null)
        {
            try
            {
                using (var SecurePassword = new SecureString())
                {
                    copiaOculta = copiaOculta ?? String.Empty;
                    copiacarbon = copiacarbon ?? String.Empty;
                    Array.ForEach(ConfigurationManager.AppSettings["ClaveCorreoSistema"].ToArray(), SecurePassword.AppendChar);

                    TemplateRender templateRender = new TemplateRender(Parent);

                    using (var smtpClient = new SmtpClient
                    {
                        Host = "mail.afari.pe", //Host Afari
                        Port = 25,              //Port Afari
                        EnableSsl = true,
                        Timeout = 60000,
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false,
                        Credentials = new NetworkCredential(remitente, SecurePassword)
                    })
                    {
                        var LstReceptores = receptor.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        LstReceptores = ConvertHelpers.RemoveDuplicates(LstReceptores);
                        //foreach (var r in LstReceptores)
                        //{
                        MailAddress from = new MailAddress(remitente, nombreRemitente);
                        //MailAddress to = new MailAddress(r);//receptor);
                        MailAddress to = new MailAddress(LstReceptores[0]);
                        using (var mailMessage = new MailMessage(from, to))
                        {
                            mailMessage.From = from;
                            //if (plantilla != "info")
                            //{
                            for (int i = 1; i < LstReceptores.Count(); i++)
                            {
                                mailMessage.To.Add(LstReceptores[i]);
                            }
                            //}
                            mailMessage.Body += $"<!DOCTYPE html>";
                            mailMessage.Body += $"<html xmlns='http://www.w3.org/1999/xhtml'>";
                            mailMessage.Body += $"<head>";
                            mailMessage.Body += $"    <meta content='text/html; charset=utf-8' http-equiv='Content-Type'>";
                            mailMessage.Body += $"    <meta content='width=device-width, initial-scale=1.0' name='viewport'>";
                            mailMessage.Body += $"    <title>@model.Titulo</title>";
                            mailMessage.Body += $"</head>";
                            mailMessage.Body += $"<body>";
                            mailMessage.Body += $"    <div style='font-family:Calibri;font-size:15px'>";
                            mailMessage.Body += $"        " + (model.Mensaje.Replace(" ", "&nbsp;").Replace("\n", "<br/>").ToString());
                            mailMessage.Body += $"    </div>";
                            mailMessage.Body += $"    <br />";
                            mailMessage.Body += $"    <p style='font-family:Calibri;font-size:15px'>    Saludos cordiales,</p>";
                            mailMessage.Body += $"    <br />";
                            if (!String.IsNullOrEmpty(model.Firma) && model.Firma.Length > 1)
                            {
                                mailMessage.Body += $"        <img src='http://afari.pe/intranet/Resources/Files/" + model.Firma + "' />";
                            }
                            mailMessage.Body += $"</body>";
                            mailMessage.Body += $"</html>";

                            mailMessage.IsBodyHtml = true;
                            mailMessage.Subject    = asunto;
                            mailMessage.Sender     = from;

                            if (plantilla != "info")
                            {
                                mailMessage.Bcc.Add(new MailAddress(remitente));
                                var LstReceptoresOcultos = copiaOculta.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                                foreach (var item in LstReceptoresOcultos)
                                {
                                    mailMessage.Bcc.Add(item);
                                }
                            }

                            if (!String.IsNullOrEmpty(copiacarbon) && copiacarbon.Length > 5)
                            {
                                var ccAddress = copiacarbon.Split(',');
                                foreach (var ccA in ccAddress)
                                {
                                    if (!String.IsNullOrEmpty(ccA))
                                    {
                                        if (ccA.Contains("@afari.pe"))
                                        {
                                            mailMessage.CC.Add(new MailAddress(ccA));
                                        }
                                        else
                                        {
                                            if (plantilla != "info")
                                            {
                                                mailMessage.CC.Add(new MailAddress(ccA));
                                            }
                                            else
                                            {
                                                mailMessage.CC.Add(new MailAddress("cc_" + ccA));
                                            }
                                        }
                                    }
                                }
                            }

                            Stream rutasArchivo = getConstanciaVisita(visita.VisitaCorretajeId);
                            if (rutasArchivo != null)
                            {
                                var data        = new Attachment(rutasArchivo, MediaTypeNames.Application.Octet);
                                var disposition = data.ContentDisposition;
                                disposition.FileName         = "Constancia_Visita_" + visita.Fecha.ToString("dd/MM/yyyy") + "_" + visita.NombreCliente + ".pdf";
                                disposition.CreationDate     = DateTime.Now;
                                disposition.ModificationDate = DateTime.Now;
                                disposition.ReadDate         = DateTime.Now;

                                if (data != null)
                                {
                                    mailMessage.Attachments.Add(data);
                                }
                            }
                            ServicePointManager.ServerCertificateValidationCallback =
                                delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                                         X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
                            { return(true); };

                            smtpClient.Send(mailMessage);
                        }
                        //}
                    }
                }
            }

            catch (Exception ex)
            {
                return(ex.Message + (ex.InnerException != null ? ex.InnerException.Message : String.Empty));
            }
            return(String.Empty);
        }