public async Task Execute(IJobExecutionContext context) { try { ResetEvent.WaitOne(); try { var emails = await _repository.GetOutboundEmailDetailsAsync(); var emailCount = emails.Count; if (emailCount <= 0) { return; } _logger.Info(emailCount + " emails retrieved"); var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; Parallel.ForEach(emails, options, email => { var smptClient = _emailConfigurator.ConfigureSmtpClient(email); var sendStatus = SendEmail(email, smptClient); UpdateEmail(email, sendStatus); }); } finally { await _repository.CommitChangesAsync(); } _logger.Info("Processing Emails Complete"); } catch (Exception ex) { _logger.Info("Exception occurred Processing Emails : " + ex); } finally { ResetEvent.Set(); } }
public void Execute() { var emails = _dataRepositoryFactory.Create <IOutboundEmailRepository>().GetOutboundEmailDetails(); var emailCount = emails.Count; if (emailCount > 0) { foreach (var email in emails) { var smptClient = _emailConfigurator.ConfigureSmtpClient(email); var sendStatus = SendEmail(email, smptClient); UpdateEmail(email, sendStatus); } ; } }