private void toolStripButtonAddModel_Click(object sender, EventArgs e) { var newName = this.toolStripComboBoxModels.Text; if (string.IsNullOrWhiteSpace(newName)) { MessageBox.Show("Must define a name for the new model.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var namesDic = this.toolStripComboBoxModels.Items .Cast <FormSendEmailConfigData.EmailModelData>() .Where(m => m.Name != null) .ToDictionary(m => m.Name); FormSendEmailConfigData.EmailModelData model; if (namesDic.ContainsKey(newName)) { if (MessageBox.Show( string.Format("Replace the model {0}", newName), "Replace model?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No) { return; } model = namesDic[newName]; } else { if (this.configData == null) { this.configData = new FormSendEmailConfigData(); } if (this.configData.EmailModels == null) { this.configData.EmailModels = new FormSendEmailConfigData.EmailModelsData(); } if (this.configData.EmailModels.Items == null) { this.configData.EmailModels.Items = new List <FormSendEmailConfigData.EmailModelData>(); } model = new FormSendEmailConfigData.EmailModelData(); model.Name = newName; this.configData.EmailModels.Items.Add(model); this.toolStripComboBoxModels.Items.Add(model); } model.HtmlContent = this.textBoxHtmlContent.Text; model.TextContent = this.textBoxTextContent.Text; model.Title = this.textBoxSubject.Text; }
private void toolStripButtonSendEmail_Click(object sender, EventArgs e) { var model = new FormSendEmailConfigData.EmailModelData(); model.HtmlContent = this.textBoxHtmlContent.Text; model.TextContent = this.textBoxTextContent.Text; model.Title = this.textBoxSubject.Text; bool ok = false; int emailsSent = 0; try { using (DebugConfig.SetDebug(this.toolStripButtonDEBUG.Checked)) { var checkedIndices = this.checkedListBoxClients.CheckedIndices.Cast <int>().ToArray(); for (int it = 0; it < checkedIndices.Length; it++) { var checkedItemIndex = checkedIndices[it]; var checkedItem = this.checkedListBoxClients.Items[checkedItemIndex]; var user = ((EmailData)checkedItem).ObjectData as User; if (user != null) { var toAddress = new MailAddress(user.Person.Email, user.Person.FullName); var subject = ProcessTemplate(model.Title, checkedItem, false); var bodyText = ProcessTemplate(model.TextContent, checkedItem, false); var bodyHtml = ProcessTemplate(model.HtmlContent, checkedItem, true); var emailMessage = EmailHelper.CreateEmailMessage(toAddress, subject, bodyText, bodyHtml); EmailHelper.SendEmail(emailMessage); emailsSent++; if (!this.toolStripButtonDEBUG.Checked) { this.checkedListBoxClients.SetItemChecked(checkedItemIndex, false); } } } } ok = true; } catch { } if (ok) { MessageBox.Show("E-mails were sent.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(string.Format("An error has occurred. Sent: {0}", emailsSent), "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } }