Exemple #1
0
        protected virtual void OnEmailSend(EmailSendEventArgs e)
        {
            EventHandler <EmailSendEventArgs> handler = EmailSendHandler;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Exemple #2
0
        public async void ConfirmarEnvioEmail(Publicacion publicacion)
        {
            ICollection <Parametro> parametros = new List <Parametro>();

            using (var context = new MailingDbContext())
            {
                parametros = context.Parametro.ToList();
            }

            var nombreServidor     = parametros.Where(x => x.Codigo == Parametros.MAIL_SERVIDOR).First().Valor;
            var puertoServidor     = parametros.Where(x => x.Codigo == Parametros.PORT_SERVIDOR).First().Valor;
            var usuario            = parametros.Where(x => x.Codigo == Parametros.USER_NAME).First().Valor;
            var password           = parametros.Where(x => x.Codigo == Parametros.USER_PASSWORD).First().Valor;
            var emailOrigenDefault = parametros.Where(x => x.Codigo == Parametros.EMAIL_ORIGEN).First().Valor;
            var emailNameDefault   = parametros.Where(x => x.Codigo == Parametros.EMAIL_NAME).First().Valor;
            var factorAgrupador    = parametros.Where(x => x.Codigo == Parametros.FACTOR_AGRUPADOR).First().Valor;

            var settings = new EmailSettings();

            settings.PrimaryDomain = nombreServidor;
            settings.PrimaryPort   = int.Parse(puertoServidor);
            settings.Username      = usuario;
            settings.Password      = password;
            settings.FromEmail     = emailOrigenDefault;
            settings.FromTitle     = emailNameDefault;

            var mailKit = new Helpers.EmailHelper.MailKit(settings);

            var destinatariosFinal = publicacion.Destinatarios.Where(x => x.Valido)
                                     .Select((x, index) => new Persona {
                Correlativo = index, Email = x.Email, Nombre = x.Nombre, Procesado = false
            }).ToList();


            int totalEnvios = Convert.ToInt32(Math.Round(Convert.ToDecimal(destinatariosFinal.Count) / Convert.ToDecimal(factorAgrupador), 0, MidpointRounding.AwayFromZero));

            if (destinatariosFinal.Count < int.Parse(factorAgrupador))
            {
                totalEnvios = 1;
            }

            EmailSendEventArgs emailPreEventData = new EmailSendEventArgs();

            emailPreEventData.Correlativo = 0;
            emailPreEventData.Data        = null;
            emailPreEventData.Total       = totalEnvios;
            OnEmailPreSend(emailPreEventData);

            string htmlBody = string.Empty;

            using (StreamReader SourceReader = System.IO.File.OpenText(publicacion.Template.Path))
            {
                htmlBody = SourceReader.ReadToEnd();
            }

            string asunto = publicacion.Subject;

            int c = 0;

            while (destinatariosFinal.Where(x => x.Procesado == false).Any())
            {
                var grupo = destinatariosFinal.Where(x => x.Procesado == false).Take(int.Parse(factorAgrupador)).ToList();

                List <KeyValuePair <string, string> > destinosGrupo = new List <KeyValuePair <string, string> >();

                foreach (var item in grupo)
                {
                    destinosGrupo.Add(new KeyValuePair <string, string>(item.Email, item.Nombre));
                }

                try
                {
                    await mailKit.SendEmailAsync(settings.FromEmail, settings.FromTitle, destinosGrupo, "", asunto, htmlBody, null, publicacion.Adjuntos.ToArray());

                    c++;

                    EmailSendEventArgs emailEventData = new EmailSendEventArgs();
                    emailEventData.Correlativo = c;
                    emailEventData.Data        = grupo;
                    emailEventData.Total       = totalEnvios;
                    OnEmailSend(emailEventData);

                    //actualizar procesados
                    foreach (var persona in grupo)
                    {
                        var destino = destinatariosFinal.Where(x => x.Correlativo == persona.Correlativo).FirstOrDefault();
                        destino.Procesado = true;
                    }
                }
                catch (Exception)
                {
                    throw;
                }

                //System.Threading.Thread.Sleep(1000);
                await Task.Delay(1000);
            }

            EmailSendEventArgs emailPostEventData = new EmailSendEventArgs();

            emailPreEventData.Correlativo = 0;
            emailPreEventData.Data        = null;
            emailPreEventData.Total       = 0;
            OnEmailPostSend(emailPostEventData);
        }