This article shows how to send mail to multiple users using parallel programming.

The following is the table design from where I am fetching employee records.

sql server empid

  1. CREATE TABLE [dbo].[Employee](
  2. [Emp_ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [varchar](50) NULL,
  4. [Email] [varchar](500) NULL,
  5. [Designation] [varchar](50) NULL,
  6. [City] [varchar](50) NULL,
  7. [State] [varchar](50) NULL,
  8. [Country] [varchar](50) NULL,
  9. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
  10. (
  11. [Emp_ID] ASC
  12. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  13. ) ON [PRIMARY]
  14. GO
The following is the data in my table:

data in table

The following is my aspx:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SendMailToMultipleUsers.Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Send Mail To Multiple Users in ASP.NET C#</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <table style="border: solid 15px blue; width: 100%; vertical-align: central;">
  11. <tr>
  12. <td style="padding-left: 50px; padding-top: 20px; padding-bottom: 20px;
  13. background-color: skyblue; font-size: 20pt; color: orangered;">
  14. Send Mail To Multiple Users in ASP.NET C#
  15. </td>
  16. </tr>
  17. <tr>
  18. <td style="text-align: left; padding-left: 50px; border: solid 1px red;">
  19. <asp:GridView ID="GridViewEmployee" runat="server" AutoGenerateColumns="False" Width="90%"
  20. BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
  21. CellPadding="4" GridLines="Both">
  22. <Columns>
  23. <asp:TemplateField HeaderText="Select">
  24. <ItemTemplate>
  25. <asp:CheckBox ID="chkSelect" runat="server" />
  26. </ItemTemplate>
  27. </asp:TemplateField>
  28. <asp:BoundField DataField="Name" HeaderText="Employee Name" />
  29. <asp:BoundField DataField="Email" HeaderText="Email" />
  30. <asp:BoundField DataField="Designation" HeaderText="Designation" />
  31. <asp:BoundField DataField="City" HeaderText="City" />
  32. <asp:BoundField DataField="State" HeaderText="State" />
  33. <asp:BoundField DataField="Country" HeaderText="Country" />
  34. </Columns>
  35. <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
  36. <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
  37. <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
  38. <RowStyle BackColor="White" ForeColor="#003399" />
  39. <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
  40. </asp:GridView>
  41. </td>
  42. </tr>
  43. <tr>
  44. <td align="right">
  45. <asp:Button ID="btnSendMail" Text="Send Mail" OnClick="btnSendMail_Click" runat="server" />
  46. </td>
  47. </tr>
  48. </table>
  49. </div>
  50. </form>
  51. </body>
  52. </html>
The following is my aspx.cs code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Net.Mail;
  10. using System.Net;
  11. using System.Threading.Tasks;
  12. namespace SendMailToMultipleUsers
  13. {
  14. public partial class Default : System.Web.UI.Page
  15. {
  16. SqlDataAdapter da;
  17. DataSet ds = new DataSet();
  18. DataTable dt = new DataTable();
  19. protected void Page_Load(object sender, EventArgs e)
  20. {
  21. if (!Page.IsPostBack)
  22. this.BindGrid();
  23. }
  24. private void BindGrid()
  25. {
  26. SqlConnection con = new SqlConnection();
  27. ds = new DataSet();
  28. con.ConnectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;";
  29. SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE", con);
  30. da = new SqlDataAdapter(cmd);
  31. da.Fill(ds);
  32. con.Open();
  33. cmd.ExecuteNonQuery();
  34. con.Close();
  35. if (ds.Tables[0].Rows.Count > 0)
  36. {
  37. GridViewEmployee.DataSource = ds.Tables[0];
  38. GridViewEmployee.DataBind();
  39. }
  40. }
  41. protected void btnSendMail_Click(object sender, EventArgs e)
  42. {
  43. DataTable dt = new DataTable();
  44. dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name", typeof(string)),
  45. new DataColumn("Email",typeof(string)) });
  46. foreach (GridViewRow row in GridViewEmployee.Rows)
  47. {
  48. if ((row.FindControl("chkSelect") as CheckBox).Checked)
  49. {
  50. dt.Rows.Add(row.Cells[1].Text, row.Cells[2].Text);
  51. }
  52. }
  53. string body = "Hi This is test Mail.<br /><br />Thanks.";
  54. Parallel.ForEach(dt.AsEnumerable(), row =>
  55. {
  56. SendEmail(row["Email"].ToString(), row["Name"].ToString(), body);
  57. });
  58. }
  59. private bool SendEmail(string To, string ToName, string body)
  60. {
  61. try
  62. {
  63. MailMessage mm = new MailMessage("[email protected]", To);
  64. mm.Subject = "Welcome " + ToName;
  65. mm.Body = body;
  66. mm.IsBodyHtml = true;
  67. SmtpClient smtp = new SmtpClient();
  68. smtp.Host = "smtp.gmail.com";
  69. smtp.EnableSsl = true;
  70. NetworkCredential NetworkCred = new NetworkCredential();
  71. NetworkCred.UserName = "[email protected]";
  72. NetworkCred.Password = "<YourGmailPassword>";
  73. smtp.UseDefaultCredentials = true;
  74. smtp.Credentials = NetworkCred;
  75. smtp.Port = 587;
  76. smtp.Send(mm);
  77. return true;
  78. }
  79. catch (Exception ex)
  80. {
  81. return false;
  82. }
  83. }
  84. }
  85. }

Now run the application.

sendmail sql server

Now select users to whom you want to send mail.

select list