Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:33:29+00:00 2026-06-13T10:33:29+00:00

The purpose of this code is to send emails in the GridView to individuals

  • 0

The purpose of this code is to send emails in the GridView to individuals whose email listed in there, so the code works fine but it does something weird. Let me explain it here, so if my GridView looks like this

ID            Name              Email
4             Mike              Mike@.com
6             John              John@.com
9             David             David@.com

Once I hit the send button,

  1. Mike receives all 3 different emails with all the 3 ID’s in the URL starting ID4, ID6, ID9 in the mail message body.
  2. John receives only a 2 emails starting ID6 and ID9
  3. David receives only 1 email which is the email for ID9 in mail message body.

What am I doing wrong here? I need each one to receive their respective email only. Can someone help? Note, the DateKeyName in the GridView is “Assigned_To” so I am not sure if I need to change that into “ID”. If I do that then It will throw an error. thanks
Here is the code:

protected void btnSendEmail_Click(object sender, EventArgs e)
    {
        MailMessage mailMessage = new MailMessage();
        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)gr.FindControl("chkItem");
            if (cb.Checked)
            {

                mailMessage.To.Add(new MailAddress(GridView1.DataKeys[gr.RowIndex]["Assigned_To"].ToString()));
                mailMessage.From = new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["fromEmailAddress"]);
                mailMessage.Priority = System.Net.Mail.MailPriority.High;
                //Text/HTML
                mailMessage.IsBodyHtml = false;
                string mySubURI = HttpContext.Current.Request.Url.AbsoluteUri.ToString().Replace("Test.aspx", "ABC.aspx");
                mySubURI += String.Format("&ID={0}", gr.Cells[0].Text);
                mailMessage.Body = "this is just test... " + " " + Environment.NewLine + mySubURI;
                mailMessage.Subject = "Test";


                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
                try
                {
                    smtpClient.Send(mailMessage);

                    //Response.Write("<B>Email Has been sent successfully.</B>");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }

            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:33:30+00:00Added an answer on June 13, 2026 at 10:33 am

    Try creating a new MailMessage after you check the checkbox, what you’re doing now is reusing the same message for each iteration and adding a new mail address without clearing the previous ones.

    protected void btnSendEmail_Click(object sender, EventArgs e)
    {
    
        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)gr.FindControl("chkItem");
            if (cb.Checked)
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.To.Add(new MailAddress(GridView1.DataKeys[gr.RowIndex]["Assigned_To"].ToString()));
                mailMessage.From = new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["fromEmailAddress"]);
                mailMessage.Priority = System.Net.Mail.MailPriority.High;
                //Text/HTML
                mailMessage.IsBodyHtml = false;
                string mySubURI = HttpContext.Current.Request.Url.AbsoluteUri.ToString().Replace("Test.aspx", "ABC.aspx");
                mySubURI += String.Format("&ID={0}", gr.Cells[0].Text);
                mailMessage.Body = "this is just test... " + " " + Environment.NewLine + mySubURI;
                mailMessage.Subject = "Test";
    
    
                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
                try
                {
                    smtpClient.Send(mailMessage);
    
                    //Response.Write("<B>Email Has been sent successfully.</B>");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
    
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The purpose of this code is to pull upgrade.zip from a central server, extract
Quick question, What have I done wrong here. The purpose of this code is
What is the purpose of these java.lang.annotation imports in this code? Why are they
I have the following code to send users a verification email, which contains some
I hope some can help me on this issue. The purpose of this email
In this following code whenever I send any command like dir or anything this
When I use this code to download this image (only used for testing purposes),
I have this block of code where when I hover (click for purposes of
I'm looking through some code for learning purposes. I'm working through this portion of
[purpose] This simple command sequence runs expected in the Windows' CMD shell: dir &

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.