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

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:

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:

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

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" >
        <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: 

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)

Share

About the Author

Dhyanga
Student
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 18:42 
Questionwhy use two gridviews? Pinmemberzhxhdean12-Jul-12 16:12 
AnswerRe: why use two gridviews? PinmemberDhyanga12-Jul-12 16:20 
GeneralRe: why use two gridviews? Pinmemberzhxhdean12-Jul-12 20:10 
GeneralRe: why use two gridviews? PinmemberDhyanga13-Jul-12 3:43 
StrNoReplyEmail is a string value. You can give any string value here. If you look into the whole project under "browse code", in default.aspx.cs file, it is defined. Or you can just define any string value.
For example:
string strNoReplyEmail = "no_reply@lawrence.edu";
Dhyanga

Answercorrected database table query PinmemberDhyanga12-Jul-12 9: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 | Mobile
Web01 | 2.8.141015.1 | Last Updated 12 Jul 2012
Article Copyright 2012 by Dhyanga
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid