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)
{
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.