void SendBills() { var exceptions = new List <KeyValuePair <Person, SmtpException> >(); var successes = new List <StatementMessage>(people.Length); PageBuilder.Prepare(emailTemplate.Text, startDate.DateTime); ProgressWorker.Execute(ui => { ui.Maximum = people.Length; foreach (var person in people) { ui.Caption = "Emailing " + person.VeryFullName; ui.Progress++; if (ui.WasCanceled) { return; } using (var message = PageBuilder.CreateMessage(person, emailTemplate.Text, startDate.DateTime)) { if (message == null) { continue; } message.To.AddRange(person.EmailAddresses.Select(e => e.MailAddress)); try { Email.Hosted.Send(message); successes.Add(message); } catch (SmtpException ex) { exceptions.Add(new KeyValuePair <Person, SmtpException>(person, ex)); } } } }, true); foreach (var info in successes) //The table can only be modified on the UI thread. { info.LogStatement(); } if (exceptions.Any()) { XtraMessageBox.Show("The following errors occurred while sending the emails:\r\n\r\n • " + exceptions.Join("\r\n\r\n • ", kvp => kvp.Key.FullName + ": " + kvp.Value.Message), "Shomrei Torah Billing", MessageBoxButtons.OK, MessageBoxIcon.Error); } }