Click here to Skip to main content
13,295,051 members (87,594 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 12 Jul 2012

Email sending from selected emails list from a GridView

, 12 Jul 2012
Rate this:
Please Sign up or sign in to vote.
Emailing the list of checked rows emails list from the gridview rows on a single button click


This article is about sending emails to any numbers of receivers from the selected rows of the gridview. The gridview has its own checkboxes in each rows and the user can select the rows by selecting the checkboxes. And then the email ids which is in one of the cell of the gridview has been used as the email id along with its first name and last name to send the email.


The database used here has been attached to the project itself. The design view here consists of two gridviews. One loads data from the database on the page load. Another is just to show the user the list of data selected from the initially loaded gridview. And there is a send button which by clicking sends email to all those selected rows data.

Using the code

The gridview named "grdViewUSelData" loads all the data from the database on the page_load. Once the data is loaded, you can select the rows by selecting the checkboxes. There is no limit of selection. Click the button "Select". This will bring all the selected rows in another gridview named "grdViewSelect". This gridview has firstname,lastname and email which we used to send the email. The format of the email along with this these fields are obtained from the function shown below and this function is the main function which sends email:

private void SendEMail(string strLastName, string strFirstName, string strEmail)
    // TODO: Pull email from config database
    MailMessage objMail = new MailMessage(strNoReplyEmail, strEmail);
    objMail.Subject = "Test Email";
    objMail.Body = @"Dear " + strFirstName + " " + strLastName + ", \n\n"
        + "This is the test email. Please do not reply to this email."
        + " \n\n"
        + "Thank you,"
        + "\n"
        + "Administrator";              

    SmtpClient sc = new SmtpClient();
    sc.Host = "";

But I have put the "SendEMail" function on the button click named "Send". So once you click that button, it will send emails to all the rows selected in the gridview.

The database connection has been stored in the web.config file under configuation as shown below:

    <add name="MyConnection" 
      connectionString="server=LocalServer;uid=sa;pwd=sa;Initial Catalog=employee"/>

The design view is shown  as below:

<asp:Panel ID="pnlShowDatabaseData" runat="server" >
    <asp:GridView ID="grdViewUSelData" runat="server" 
            GridLines="None" AutoGenerateColumns="false" 
            DataKeyNames = "id" EnableViewState="true" >
                    <asp:CheckBox ID="chkStatus" runat="server" />
            <asp:BoundField DataField="id" HeaderText="ID" />
            <asp:BoundField DataField ="fname" HeaderText="First Name" />
            <asp:BoundField DataField="lname" HeaderText="Last Name" />
            <asp:BoundField DataField="email" HeaderText="Email" />
    <asp:Button ID="btnSelect" runat="server" Text="Select" 
        onclick="btnSelect_Click" />
<asp:Panel ID="pnlSelectData" runat="server" visible="false">
    <asp:GridView ID="grdViewUpDate" runat="server" 
            GridLines="None" AutoGenerateColumns="false" DataKeyNames="id" >
            <asp:BoundField DataField="id" HeaderText="ID" />
            <asp:BoundField DataField = "fname" HeaderText="First Name" />
            <asp:BoundField DataField="lname" HeaderText="Last Name" />
            <asp:BoundField DataField="email" HeaderText="Email" />
    <asp:Button ID="btnSend" runat="server" Text="Send Email" 
        onclick="btnSend_Click" />

On the btnSend click, the gridview "grdViewSelect" has been in loop for all its selected rows and called the above "sendEMail" function to send email for each email retrieved from the gridview as below: 

string fname, lname, email;
fname = lname = email = "";
foreach (GridViewRow grdRow in grdViewUpDate.Rows)
    fname = grdRow.Cells[1].Text;
    lname = grdRow.Cells[2].Text;
    email = grdRow.Cells[3].Text;
    SendEMail(lname, fname, email);

This way we can send multiple emails, taken email IDs from the checked rows of the gridview, in a single button click.

I hope it helps you.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nepal Nepal
Nothing is particularly hard if you divide it into small jobs. -- Henry Ford

You may also be interested in...

Comments and Discussions

QuestionI can't download Pin
Jun chul shin9-Apr-13 19:42
memberJun chul shin9-Apr-13 19:42 
I just started studying.

please send me all code. plz~~~
Questionwhy use two gridviews? Pin
zhxhdean12-Jul-12 17:12
memberzhxhdean12-Jul-12 17:12 
AnswerRe: why use two gridviews? Pin
Dhyanga12-Jul-12 17:20
memberDhyanga12-Jul-12 17:20 
GeneralRe: why use two gridviews? Pin
zhxhdean12-Jul-12 21:10
memberzhxhdean12-Jul-12 21:10 
GeneralRe: why use two gridviews? Pin
Dhyanga13-Jul-12 4:43
memberDhyanga13-Jul-12 4:43 
Answercorrected database table query Pin
Dhyanga12-Jul-12 10:39
memberDhyanga12-Jul-12 10:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 12 Jul 2012
Article Copyright 2012 by Dhyanga
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid