Click here to Skip to main content
15,867,686 members
Articles / Web Development / ASP.NET
Tip/Trick

Email sending from selected emails list from a GridView

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
12 Jul 2012CPOL2 min read 26.4K   8   7
Emailing the list of checked rows emails list from the gridview rows on a single button click

Introduction

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.

Background

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:

C#
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 = "www.hotmail.com";
    sc.Send(objMail);
} 

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:

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

The design view is shown  as below:

C++
<asp:Panel ID="pnlShowDatabaseData" runat="server" >
    <asp:GridView ID="grdViewUSelData" runat="server" 
            GridLines="None" AutoGenerateColumns="false" 
            DataKeyNames = "id" EnableViewState="true" >
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkStatus" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <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" />
        </Columns>
    </asp:GridView>
    <asp:Button ID="btnSelect" runat="server" Text="Select" 
        onclick="btnSelect_Click" />
</asp:Panel>
<asp:Panel ID="pnlSelectData" runat="server" visible="false">
    <asp:GridView ID="grdViewUpDate" runat="server" 
            GridLines="None" AutoGenerateColumns="false" DataKeyNames="id" >
        <Columns>
            <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" />
        </Columns>
    </asp:GridView> 
    <asp:Button ID="btnSend" runat="server" Text="Send Email" 
        onclick="btnSend_Click" />
</asp:Panel>

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: 

C++
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.

License

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


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

Comments and Discussions

 
Questionplease send me the code Pin
Member 138601355-Jun-18 11:16
Member 138601355-Jun-18 11:16 
QuestionI can't download Pin
Jun chul shin9-Apr-13 18:42
Jun chul shin9-Apr-13 18:42 
Questionwhy use two gridviews? Pin
zhxhdean12-Jul-12 16:12
zhxhdean12-Jul-12 16:12 
AnswerRe: why use two gridviews? Pin
Dhyanga12-Jul-12 16:20
Dhyanga12-Jul-12 16:20 
GeneralRe: why use two gridviews? Pin
zhxhdean12-Jul-12 20:10
zhxhdean12-Jul-12 20:10 
GeneralRe: why use two gridviews? Pin
Dhyanga13-Jul-12 3:43
Dhyanga13-Jul-12 3:43 
Answercorrected database table query Pin
Dhyanga12-Jul-12 9:39
Dhyanga12-Jul-12 9: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.