title: Sending multiple emails at checked checkbox in gridview not working
body: <p>I would like some help with the following problem. I'm trying to send emails to users who are checked in a gridview. I've searched around and found many topics that look alike but I couldn't find a working solution. Well I know i'm missing something, maybe in my select statement. But I can't figure it out. </p>
  
  <p>My code shows a gridview with let's say 2 users, many items and the email addresses from the users. I have selected 1 item for each user so 2 emails should be send. 2 mails are sent, but only to 1 email address, the email address he meets first in the database. I tried searching on userID but couldn't make that work, i'm still in my first few months at developping in .net. </p>
  
  <p>See here my code:</p>
  
  <pre><code>    public void Email()
      {
          string conn = "Data Source=pc-...";
          LabelSendGridAbove.Text = "&lt;b&gt;Title&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; Text... &lt;br /&gt;&lt;br /&gt; ";
  
          LabelSendGridBetween.Text = "&lt;br /&gt;&lt;br /&gt; More text. " +
               "&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; Regards, &lt;br /&gt;&lt;br /&gt; " ;
  
          LabelSendGridUnder.Text = "&lt;br /&gt;&lt;br /&gt;--------------------------------------";
  
  
          System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(conn);
          sqlConn.Open();
          SqlCommand sendGrid =
              new SqlCommand("SELECT statement for gridview which will be send in the email ", sqlConn);
  
          GridView grd = new GridView();
  
          // Css for the gridview which will be send in mail     
          grd.BorderStyle = System.Web.UI.WebControls.BorderStyle.None;
          grd.GridLines = GridLines.None;
          grd.RowStyle.HorizontalAlign = HorizontalAlign.Center;
          grd.Width = 600;
  
          foreach (DataControlField field in grd.Columns)
          {
              field.ItemStyle.Width = Unit.Percentage(100 / grd.Columns.Count);
          }
  
          if (sendGrid != null)
          {
              grd.DataSource = sendGrid.ExecuteReader();
              grd.DataBind();
          }
  
          StringBuilder sb = new StringBuilder();
  
          StringWriter sw = new StringWriter(sb);
          HtmlTextWriter htw = new HtmlTextWriter(sw);
          grd.RenderControl(htw);
  
          sqlConn.Close();
  
  
          foreach (GridViewRow r in GridViewOrder.Rows)
          {
              if (((CheckBox)r.Cells[0].FindControl("CheckBoxGetProduct")).Checked == true )
              {
                  System.Data.SqlClient.SqlConnection sqlConn3 = new System.Data.SqlClient.SqlConnection(conn);
                  sqlConn3.Open();
  
                      if (((Label)r.Cells[0].FindControl("LabelEmailUsr")) != null)
                      {
                          Label txtUser = (Label)GridViewOrder.FindControl("LabelEmailUsr");
                          if (txtUser != null)
                          {
                              LabelTestMail.Visible = true;
                              LabelTestMail.Text = txtUser.Text.ToString();
                          }
  
                          SqlCommand emailAdres = new SqlCommand("SELECT tblUsers.tUserEmail FROM tblUsers", sqlConn3);
  
                          GridViewOrder.DataBind();
  
                          string email = Convert.ToString(emailAdres.ExecuteScalar());
                          LabelTestMail.Text = email;
  
                          try
                          {
                              MailMessage mail = new MailMessage();
                              mail.To.Add(email.ToString());
                              mail.Bcc.Add("SomeEmail");
                              mail.From = new MailAddress("FromWho");
                              mail.Subject = "SomeSubject";
                              string Body = LabelSendGridAbove.Text + sb.ToString() + LabelSendGridBetween.Text + "&lt;br /&gt;&lt;img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 &gt;" + LabelSendGridUnder.Text + "&lt;BR&gt;";
  
                              AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
                              LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\logo\Logo.jpg");
                              imagelink.ContentId = "imageId";
                              imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
                              htmlView.LinkedResources.Add(imagelink);
                              mail.AlternateViews.Add(htmlView);
                              SmtpClient smtp = new SmtpClient("relay.skynet.be");
                              smtp.Send(mail);
                          }
                          catch (Exception ex)
                          {
                              Response.Write(ex.Message);
                          }
                      }
              }              
        }
    } 
  </code></pre>
  
  <p>I hope there aren't too many mistakes in this code. But if anyone knows what I should add to the code to make it work so it will send the 2 emails to the 2 different email addresses that would be very nice. </p>
  
  <p>Thank you.</p>