Click here to Skip to main content
Click here to Skip to main content

Email sending from selected emails list from a GridView

, 12 Jul 2012 CPOL
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

Comments and Discussions

QuestionI can't download PinmemberJun chul shin9-Apr-13 19:42 
Questionwhy use two gridviews? Pinmemberzhxhdean12-Jul-12 17:12 
AnswerRe: why use two gridviews? PinmemberDhyanga12-Jul-12 17:20 
I have attached the whole project. Please find all the codes under browse code. And to the question about used two gridviews, there is absolutely no need to use two gridviews. Somebody had asked me how to show the selected rows in another gridview, just for that sake I used it. No need to use two gridviews. Please let me know if you still can't find all the codes. I'm happy to send you one if you can't download from the site itself.

GeneralRe: why use two gridviews? Pinmemberzhxhdean12-Jul-12 21:10 
GeneralRe: why use two gridviews? PinmemberDhyanga13-Jul-12 4:43 
Answercorrected database table query PinmemberDhyanga12-Jul-12 10:39 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150224.1 | Last Updated 12 Jul 2012
Article Copyright 2012 by Dhyanga
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid