Пример #1
0
        public async static Task MandaCorreosAsync()
        {
            try
            {
                Mail mail = new Mail();

                resultado = await mail.MandaEmail();

                //return resultado;
            }
            catch (Exception ex)
            {
                Console.Error.Write(ex.Message);
                //throw;
            }
        }
Пример #2
0
        public async Task <ExcecutionResult> MandaEmail()
        {
            try
            {
                ExcecutionResult Result = new ExcecutionResult();
                Autofac.ContainerConfig.Start();

                var ColaMensajes = await ReadMail();

                var Correo = await ReadSMTPConfig();

                using (SmtpClient client = new SmtpClient())
                {
                    client.EnableSsl             = false;
                    client.UseDefaultCredentials = false;
                    client.Credentials           = new NetworkCredential(Correo.Usuario, Correo.Password);
                    client.Host                     = Correo.Host;
                    client.Port                     = (int)Correo.Puerto;
                    client.DeliveryMethod           = SmtpDeliveryMethod.Network;
                    client.ServicePoint.MaxIdleTime = 1;
                    client.Timeout                  = 10000;

                    foreach (var destino in ColaMensajes)
                    {
                        MailMessage msg = new MailMessage();

                        msg.From = new MailAddress(Correo.Correo.ToString());
                        msg.To.Add(destino.Para.ToString());
                        msg.Subject    = destino.Asunto;
                        msg.Body       = destino.Mensaje;
                        msg.IsBodyHtml = destino.EsHTML.Equals("SI");
                        msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

                        try
                        {
                            client.Send(msg);
                            destino.EstadoEnvio = "ENVIADO";
                            destino.FechaEnvio  = DateTime.Now;
                            Result.CorreosEnviados++;
                        }
                        catch (Exception ex)
                        {
                            destino.EstadoEnvio = "ERROR";
                            //destino.FechaEnvio = DateTime.Now;

                            Result.CorreosNoEnviados++;
                            Result.Referencia += "-> [" + ex.Message + "<" + ex.InnerException + ">" + "]";
                        }
                    }

                    var update = UpdateMail(ColaMensajes);
                }

                return(Result);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error fatal: " + ex.Message + " / " + ex.InnerException.Message);
                throw;
            }
        }