locked
Send email to seleccted user i gridview RRS feed

  • Question

  • User1967812301 posted

    Hello everybody i am new to asp.net .I have a scenario that ..

    Gridview which is showing the customer information like ,"Cust_ID","Cust_Name","Email" and one checkbox with each row and send email button below the gridview .

    I want to implement that when user select particular rows from gridview and press send email button.Email will go to the all selected users.

    Thursday, August 21, 2014 8:23 AM

Answers

  • User2103319870 posted

    You need to look through the rows in Gridview and get the email address from selected rows based on checkbox selection.

    Please see the sample code

    protected void btnSendMail_Click(object sender, EventArgs e)
            {
                //Array list to hold selected email ids
                ArrayList emailArray = new ArrayList();
                //Looping through rows of GridView
                foreach (GridViewRow gr in GridView1.Rows)
                {
                    //Creating checkbox object using the find control method
                    CheckBox cb = (CheckBox)gr.FindControl("chkItem");
                    //Checking whether checkbox is checked or not
                    if (cb.Checked)
                    {
                        //If checked Adding email id to Arraylist
                        emailArray.Add(GridView1.DataKeys[gr.RowIndex]["EmailAddress"].ToString());
                    }
                }
                //Looping through the email id list
                foreach (string email in emailArray)
                {
                    //Set email user name - Change this as per your settings
                    const string username = "testuser@googlemail.com";
                    //Set the email password - Change this as per your settings
                    const string password = "xxxxxxxxx";
                    SmtpClient smtpclient = new SmtpClient();
                    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                    //Set the email from address of mail message -  Change this as per your settings
                    MailAddress fromaddress = new MailAddress("testuser@googlemail.com");
                    //Set the email smtp host -  Change this as per your settings
                    smtpclient.Host = "smtp.gmail.com";
                    //Set the email client port -  Change this as per your settings
                    smtpclient.Port = 587;
                    mail.From = fromaddress;
                    //Adding email id from array list as to address of email
                    mail.To.Add(email);
                    //Set the email subject
                    mail.Subject = ("Test Subject");
                    mail.IsBodyHtml = true;
                    //Set the email body - Change this as per your logic
                    mail.Body = "This is test Mail";
                    smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
                    try
                    {
                        //Sending Email
                        smtpclient.Send(mail);
                        Response.Write("<B>Email Has been sent successfully.</B>");
                    }
                    catch (Exception ex)
                    {
                        //Catch if any exception occurs
                        Response.Write(ex.Message);
                    }
                }
            }

    You can refer the below link for more details

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 21, 2014 11:02 AM

All replies

  • User953931160 posted

    Hi Junaid,

    On button_click event by using findcontrols in the method loop through all the checkbox controls and find the value is true or not.

    if is true save all the respective emailId of that row into a list or a array and then loop through the list or a array and send the email by using sftp settings.

    for example :(sending mail) you change the information according to your requirement:

                    MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                    mail.From = new MailAddress("your_email_address@gmail.com");
                    mail.To.Add("to_address");
                    mail.Subject = "Test Mail";
                    mail.Body = "test mail body";

                    SmtpServer.Port = 587; // for gmail.
                    SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                    SmtpServer.EnableSsl = true;

                    SmtpServer.Send(mail);

     

    Please try these steps it may help you.

    Regards,

    Santosh Kumar Dash

    Thursday, August 21, 2014 9:07 AM
  • User2103319870 posted

    You need to look through the rows in Gridview and get the email address from selected rows based on checkbox selection.

    Please see the sample code

    protected void btnSendMail_Click(object sender, EventArgs e)
            {
                //Array list to hold selected email ids
                ArrayList emailArray = new ArrayList();
                //Looping through rows of GridView
                foreach (GridViewRow gr in GridView1.Rows)
                {
                    //Creating checkbox object using the find control method
                    CheckBox cb = (CheckBox)gr.FindControl("chkItem");
                    //Checking whether checkbox is checked or not
                    if (cb.Checked)
                    {
                        //If checked Adding email id to Arraylist
                        emailArray.Add(GridView1.DataKeys[gr.RowIndex]["EmailAddress"].ToString());
                    }
                }
                //Looping through the email id list
                foreach (string email in emailArray)
                {
                    //Set email user name - Change this as per your settings
                    const string username = "testuser@googlemail.com";
                    //Set the email password - Change this as per your settings
                    const string password = "xxxxxxxxx";
                    SmtpClient smtpclient = new SmtpClient();
                    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                    //Set the email from address of mail message -  Change this as per your settings
                    MailAddress fromaddress = new MailAddress("testuser@googlemail.com");
                    //Set the email smtp host -  Change this as per your settings
                    smtpclient.Host = "smtp.gmail.com";
                    //Set the email client port -  Change this as per your settings
                    smtpclient.Port = 587;
                    mail.From = fromaddress;
                    //Adding email id from array list as to address of email
                    mail.To.Add(email);
                    //Set the email subject
                    mail.Subject = ("Test Subject");
                    mail.IsBodyHtml = true;
                    //Set the email body - Change this as per your logic
                    mail.Body = "This is test Mail";
                    smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
                    try
                    {
                        //Sending Email
                        smtpclient.Send(mail);
                        Response.Write("<B>Email Has been sent successfully.</B>");
                    }
                    catch (Exception ex)
                    {
                        //Catch if any exception occurs
                        Response.Write(ex.Message);
                    }
                }
            }

    You can refer the below link for more details

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 21, 2014 11:02 AM
  • Saturday, August 30, 2014 3:37 AM