void ApplyTemplate(string templateName, params MelaveMalkaInvitation[] people) { if (people.Length == 0) { Dialog.ShowError("Please select people."); return; } if (people.Length > 1 && !Dialog.Confirm($"Would you like to set {people.Length} people to receive the {templateName} template?")) { return; } Program.LoadTable <MelaveMalkaInfo>(); //Used by the templates foreach (var recipient in people) { try { using (var message = razor.CreateMessage(recipient, templateName)) { recipient.EmailSubject = message.Subject; recipient.EmailSource = message.Body; } } catch (Exception ex) { Dialog.ShowError($"An error occurred while applying the template to {recipient.Person.VeryFullName}:\n{ex}"); return; } } }
private void emailTemplateList_ListItemClick(object sender, ListItemClickEventArgs e) { var actualCallers = ConfirmSendEmailTemplate(); if (actualCallers == null) { return; } Program.LoadTables(MelaveMalkaInfo.Schema, MelaveMalkaSeat.Schema, MelaveMalkaInvitation.Schema); //Used by the templates var template = emailTemplateList.Strings[e.Index]; ProgressWorker.Execute(progress => { progress.Maximum = actualCallers.Count * 2; //Two steps per caller foreach (var caller in actualCallers) { if (progress.WasCanceled) { return; } progress.Caption = "Creating spreadsheet for " + caller.Name; string attachmentPath; do { attachmentPath = Path.GetTempFileName(); File.Delete(attachmentPath); attachmentPath = Path.ChangeExtension(attachmentPath, ".xls"); //OleDB cannot write XLS with other extensions } while (File.Exists(attachmentPath)); try { caller.CreateCallList(attachmentPath); if (progress.WasCanceled) { return; } progress.Progress++; progress.Caption = "Emailing " + caller.Name; using (var message = razor.CreateMessage(caller, template)) { message.From = Email.JournalAddress; message.To.Add(caller.EmailAddresses); //Comma-separated string message.Attachments.Add(new Attachment(attachmentPath, new ContentType { MediaType = "application/vnd.ms-excel", Name = "Call List for " + caller.Name + ".xls" })); Email.Default.Send(message); } } finally { File.Delete(attachmentPath); } progress.Progress++; } }, true); }