How to send email for selected record in Grid view using check box column?


In this article I have explained about how to send e-mail to the selected user in the grid view check box. In the example application I used check box column for user select option after user click “send email” button I check which user option is clicked I send mail to that user mail id only.

How to create check box column in grid view?
You can create check box column in the grid view using template field. Put the check box control in to the template field item template field.

How to select all check box in the grid view when user click header column of check box control?
Use javascript function to check/uncheck all check boxes in the grid view when user click header check box like below code
Client side


<script language="javascript" type="text/javascript">
function SelectAll(id) {
var frm = document.forms[0];
for (i = 0; i < frm.elements.length; i++) {
if (frm.elements[i].type == "checkbox") {
frm.elements[i].checked = document.getElementById(id).checked;
}
}
}
</script>

How to send email based on Grid View selected checkbox?
After user click send email button I have validate user select atleast one check box and also validate user enter text in the messeage box or not using below code

protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "")
{
Page.RegisterStartupScript("Alert Message", "<script language='javascript'>alert('Enter Message in Text Box');</script>");
return;
}

//Checkther whether atleast one check box is selected or not
for(int i=0;i<=GridView1.Rows.Count-1;i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
if (Ckbox.Checked == true)
{
k++;
}
}

if (k == 0)
{
Page.RegisterStartupScript("Alert Message", "<script language='javascript'>alert('Select atleast one Check box further to proceed');</script>");
return;
}

for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
string toemail = GridView1.Rows[i].Cells[3].Text;
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
if (Ckbox.Checked == true)
{
sendMail(toemail);
}
}
TextBox1.Text = "";
}

void sendMail(String toemail)
{
try
{
//Below I mention From id(Gmail), To mail ID, Subject of the mail, Mail Message
MailMessage msg = new MailMessage("fromid@gmail.com", toemail , "Testing Mail", TextBox1.Text);
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
//Change your gmail user id and password for send email
NetworkCredential NetCrd = new NetworkCredential("fromid@gmail.com", "fromidpassword");
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}

Source Code Detail:
Here with I have attached source code to for grid view check box column with email option. Download it and try to send email for various users at the same time single button click.
Front End : ASP.NET
Code Behind : C#

Conclusion:
I hope this Article is help to you send email for multiple user.


Attachments

  • GridView_ChkBox_MailSend (42891-291029-GridView_MailSent_chkoption.rar)
  • Comments

    Guest Author: Karthi19 Jun 2014

    Hi i need a .net code for How to send email for selected record in Grid view using check box column?

    can you send me please



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: