Ejemplo n.º 1
0
        private void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            _timer.Enabled = false;
            int lastMessageId = -1;

            try
            {
                var messages = _emailMessageService.GetPendingEmailMessages().ToList();

                if (messages.Any())
                {
                    using (SmtpClient smtp = new SmtpClient(Host, Port))
                    {
                        smtp.EnableSsl             = EnableSsl;
                        smtp.UseDefaultCredentials = UseDefaultCredentials;
                        smtp.Credentials           = new NetworkCredential(UserName, Password);

                        foreach (var message in messages)
                        {
                            lastMessageId = message.Id;
                            var mailMessage = new MailMessage(UserName, message.To, message.Subject, message.Message);
                            mailMessage.IsBodyHtml = true;
                            smtp.Send(mailMessage);
                            _emailMessageService.RegisterEmailSent(lastMessageId);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (lastMessageId != -1)
                {
                    _emailMessageService.RegisterEmailError(lastMessageId);
                }

                _logManager.DefaultLogger.Error.Write("Mentoring.EmailService.TimerElapsed", ex);
            }

            _timer.Enabled = true;
        }