// email selected users public static void SendEmailToSelectedUsers(GridView GridView1, TextBox txbMailFrom, TextBox txb_Subject, FredCK.FCKeditorV2.FCKeditor WYSIWYGEditor_EmailBody, RadioButtonList rbt_BodyTextType, RadioButtonList rbt_Importance, HyperLink Msg) { // for each row in the gridview foreach (GridViewRow row in GridView1.Rows) { // find checked checkboxes in gridview CheckBox cb = (CheckBox)row.FindControl("chkRows"); // if checkboxes are nut null and at least one of them are checked if (cb == null || !cb.Checked) continue; try { // declare new mailer System.Net.Mail.MailMessage MyMailer = new System.Net.Mail.MailMessage(); // grab the email address of the selected checkboxes string Email = GridView1.DataKeys[row.RowIndex].Values["Email"].ToString(); // grab the user name of the selected checkboxes string Name = GridView1.DataKeys[row.RowIndex].Values["userName"].ToString(); //MyMailer.To.Add(Email); MyMailer.Bcc.Add(Email); MyMailer.From = new MailAddress(txbMailFrom.Text); MyMailer.Subject = txb_Subject.Text; MyMailer.Body = "Dear " + Name + ", <br/><br/>" + WYSIWYGEditor_EmailBody.Value; MyMailer.IsBodyHtml = Convert.ToBoolean(rbt_BodyTextType.SelectedValue); // grab the selected priority level switch (rbt_Importance.SelectedValue) { case "Low": MyMailer.Priority = MailPriority.Low; break; case "Normal": MyMailer.Priority = MailPriority.Normal; break; case "High": MyMailer.Priority = MailPriority.High; break; } // create new smtp client SmtpClient client = new SmtpClient(); // send the email client.Send(MyMailer); // display success message Msg.Text = "<strong>E-MAIL</strong> message <strong>SENT</strong> successfully!..."; Msg.Visible = true; } catch (Exception ex) { // display error sending message Msg.Text = "ERROR: Problem sending email! " + ex.Message; Msg.Visible = true; } } }
public static void SendEmailToSelectedRole(DropDownList ddlSendMailToSelectedRole, TextBox txbMailFrom, TextBox txb_Subject, FredCK.FCKeditorV2.FCKeditor WYSIWYGEditor_EmailBody, RadioButtonList rbt_BodyTextType, RadioButtonList rbt_Importance, HyperLink Msg) { try { // for each user in the membership database foreach (MembershipUser mu in Membership.GetAllUsers()) { // if a user in database is in the selected role if (Roles.IsUserInRole(mu.UserName, ddlSendMailToSelectedRole.SelectedItem.Text)) { // declare new mailer System.Net.Mail.MailMessage MyMailer = new System.Net.Mail.MailMessage(); //MyMailer.To.Add(mu.Email); MyMailer.Bcc.Add(mu.Email); MyMailer.From = new MailAddress(txbMailFrom.Text); MyMailer.Subject = txb_Subject.Text; MyMailer.Body = WYSIWYGEditor_EmailBody.Value; MyMailer.IsBodyHtml = Convert.ToBoolean(rbt_BodyTextType.SelectedValue); // grab the selected priority level switch (rbt_Importance.SelectedValue) { case "Low": MyMailer.Priority = MailPriority.Low; break; case "Normal": MyMailer.Priority = MailPriority.Normal; break; case "High": MyMailer.Priority = MailPriority.High; break; } // create new smtp client SmtpClient client = new SmtpClient(); // send the email client.Send(MyMailer); // display success message Msg.Text = "<strong>E-MAIL</strong> message <strong>SENT</strong> successfully!..."; Msg.Visible = true; } } } catch (Exception ex) { // display error sending message Msg.Text = "ERROR: Problem sending email! " + ex.Message; Msg.Visible = true; } }