Send Mail to Multiple Users Using Parallel Programming in ASP.Net C#

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  = ONON [PRIMARY]  
  13. ON [PRIMARY]  
  14.   
  15. 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.   
  25.                                     <ItemTemplate>  
  26.                                         <asp:CheckBox ID="chkSelect" runat="server" />  
  27.                                     </ItemTemplate>  
  28.                                 </asp:TemplateField>  
  29.                                 <asp:BoundField DataField="Name" HeaderText="Employee Name" />  
  30.                                 <asp:BoundField DataField="Email" HeaderText="Email" />  
  31.                                 <asp:BoundField DataField="Designation" HeaderText="Designation" />  
  32.                                 <asp:BoundField DataField="City" HeaderText="City" />  
  33.                                 <asp:BoundField DataField="State" HeaderText="State" />  
  34.                                 <asp:BoundField DataField="Country" HeaderText="Country" />  
  35.                             </Columns>  
  36.                             <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />  
  37.                             <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />  
  38.                             <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />  
  39.                             <RowStyle BackColor="White" ForeColor="#003399" />  
  40.                             <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />  
  41.                         </asp:GridView>  
  42.                     </td>  
  43.                 </tr>  
  44.                 <tr>  
  45.                     <td align="right">  
  46.                         <asp:Button ID="btnSendMail" Text="Send Mail" OnClick="btnSendMail_Click" runat="server" />  
  47.                     </td>  
  48.                 </tr>  
  49.             </table>  
  50.         </div>  
  51.     </form>  
  52. </body>  
  53. </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.   
  13. namespace SendMailToMultipleUsers  
  14. {  
  15.     public partial class Default : System.Web.UI.Page  
  16.     {  
  17.         SqlDataAdapter da;  
  18.         DataSet ds = new DataSet();  
  19.         DataTable dt = new DataTable();  
  20.   
  21.         protected void Page_Load(object sender, EventArgs e)  
  22.         {  
  23.             if (!Page.IsPostBack)  
  24.                 this.BindGrid();  
  25.         }  
  26.   
  27.         private void BindGrid()  
  28.         {  
  29.             SqlConnection con = new SqlConnection();  
  30.             ds = new DataSet();  
  31.             con.ConnectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;";  
  32.             SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE", con);  
  33.   
  34.             da = new SqlDataAdapter(cmd);  
  35.             da.Fill(ds);  
  36.             con.Open();  
  37.             cmd.ExecuteNonQuery();  
  38.             con.Close();  
  39.   
  40.             if (ds.Tables[0].Rows.Count > 0)  
  41.             {  
  42.                 GridViewEmployee.DataSource = ds.Tables[0];  
  43.                 GridViewEmployee.DataBind();  
  44.             }  
  45.         }  
  46.   
  47.         protected void btnSendMail_Click(object sender, EventArgs e)  
  48.         {  
  49.             DataTable dt = new DataTable();  
  50.             dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"typeof(string)),  
  51.                         new DataColumn("Email",typeof(string)) });  
  52.   
  53.   
  54.             foreach (GridViewRow row in GridViewEmployee.Rows)  
  55.             {  
  56.                 if ((row.FindControl("chkSelect"as CheckBox).Checked)  
  57.                 {  
  58.                     dt.Rows.Add(row.Cells[1].Text, row.Cells[2].Text);  
  59.                 }  
  60.             }  
  61.   
  62.   
  63.             string body = "Hi This is test Mail.<br /><br />Thanks.";  
  64.   
  65.   
  66.             Parallel.ForEach(dt.AsEnumerable(), row =>  
  67.             {  
  68.                 SendEmail(row["Email"].ToString(), row["Name"].ToString(), body);  
  69.             });  
  70.         }  
  71.   
  72.         private bool SendEmail(string To, string ToName, string body)  
  73.         {  
  74.             try  
  75.             {  
  76.                 MailMessage mm = new MailMessage("[email protected]", To);  
  77.                 mm.Subject = "Welcome " + ToName;  
  78.                 mm.Body = body;  
  79.                 mm.IsBodyHtml = true;  
  80.                 SmtpClient smtp = new SmtpClient();  
  81.                 smtp.Host = "smtp.gmail.com";  
  82.                 smtp.EnableSsl = true;  
  83.                 NetworkCredential NetworkCred = new NetworkCredential();  
  84.                 NetworkCred.UserName = "[email protected]";  
  85.                 NetworkCred.Password = "<YourGmailPassword>";  
  86.                 smtp.UseDefaultCredentials = true;  
  87.                 smtp.Credentials = NetworkCred;  
  88.                 smtp.Port = 587;  
  89.                 smtp.Send(mm);  
  90.                 return true;  
  91.             }  
  92.             catch (Exception ex)  
  93.             {  
  94.                 return false;  
  95.             }  
  96.         }  
  97.     }  
  98. }  

Now run the application.

sendmail sql server

Now select users to whom you want to send mail.

select list


Similar Articles