Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Send mail to multiple users using Parallel programming in ASP.NET

Viewed:  1952 
Posted On:  14/02/2015 08:16:15 
How we can send mail to users in c# ? How we can send mail to multiple users using Parallel programming in c#? 

In this article I am going to show how we can send mail to multiple users by using parallel programing concept.

Below is the table design from which I am fetching employee records.


Image 1.

CREATE TABLE [dbo].[Employee](
          [Emp_ID] [int] IDENTITY(1,1) NOT NULL,
          [Name] [varchar](50) NULL,
          [Email] [varchar](500) NULL,
          [Designation] [varchar](50) NULL,
          [City] [varchar](50) NULL,
          [State] [varchar](50) NULL,
          [Country] [varchar](50) NULL,
          [Emp_ID] ASC

   ) ON [PRIMARY] 


Data in my Table:


Image 2.

Now my aspx is:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SendMailToMultipleUsers.Default" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <title>Send Mail To Multiple Users in ASP.NET C#</title>
    <form id="form1" runat="server">
            <table style="border: solid 15px blue; width: 100%; vertical-align: central;">
                    <td style="padding-left: 50px; padding-top: 20px; padding-bottom: 20px;
                            background-color: skyblue; font-size: 20pt; color: orangered;">
                        Send Mail To Multiple Users in ASP.NET C#
                    <td style="text-align: left; padding-left: 50px; border: solid 1px red;">
                        <asp:GridView ID="GridViewEmployee" runat="server" AutoGenerateColumns="False" Width="90%"
                            BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
                            CellPadding="4" GridLines="Both">
                                <asp:TemplateField HeaderText="Select">
                                        <asp:CheckBox ID="chkSelect" runat="server" />

<asp:BoundField DataField="Name" HeaderText="Employee Name" />
                                <asp:BoundField DataField="Email" HeaderText="Email" />
                                <asp:BoundField DataField="Designation" HeaderText="Designation" />
                                <asp:BoundField DataField="City" HeaderText="City" />
                                <asp:BoundField DataField="State" HeaderText="State" />
                                <asp:BoundField DataField="Country" HeaderText="Country" />
                           <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                            <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                            <RowStyle BackColor="White" ForeColor="#003399" />
                            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                    <td align="right">
                        <asp:Button ID="btnSendMail" Text="Send Mail" OnClick="btnSendMail_Click" runat="server" />


My aspx.cs code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Net;
using System.Threading.Tasks;
namespace SendMailToMultipleUsers
    public partial class Default : System.Web.UI.Page
        SqlDataAdapter da;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
            if (!Page.IsPostBack)
        private void BindGrid()
            SqlConnection con = new SqlConnection();
            ds = new DataSet();
            con.ConnectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;";
            SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE", con);
            da = new SqlDataAdapter(cmd);
            if (ds.Tables[0].Rows.Count > 0)
                GridViewEmployee.DataSource = ds.Tables[0];
        protected void btnSendMail_Click(object sender, EventArgs e)
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name", typeof(string)),
                        new DataColumn("Email",typeof(string)) });
            foreach (GridViewRow row in GridViewEmployee.Rows)
                if ((row.FindControl("chkSelect") as CheckBox).Checked)
                    dt.Rows.Add(row.Cells[1].Text, row.Cells[2].Text);
            string body = "Hi This is test Mail.<br /><br />Thanks.";  

            Parallel.ForEach(dt.AsEnumerable(), row =>
                SendEmail(row["Email"].ToString(), row["Name"].ToString(), body);
        private bool SendEmail(string To, string ToName, string body)
                MailMessage obj= new MailMessage("", To);
                obj.Subject = "Welcome " + ToName;
                obj.Body = body;
                obj.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential();

                NetworkCred.UserName = "";
                NetworkCred.Password = "<YourGmailPassword>";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                return true;
            catch (Exception ex)
                return false;

Now run your application:


Image 3.

Now select users whom do you want to send mail.


Image 4.

By dotnet  On  06/04/2016 01:33:10

Send Confirmation Email After Registration in
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us