Sending multiple emails at checked checkbox in gridview not working
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.
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.
See here my code:
public void Email()
{
string conn = "Data Source=pc-...";
LabelSendGridAbove.Text = "<b>Title</b><br /><br /> Text... <br /><br /> ";
LabelSendGridBetween.Text = "<br /><br /> More text. " +
"<br /><br /><br /> Regards, <br /><br /> " ;
LabelSendGridUnder.Text = "<br /><br />--------------------------------------";
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.S开发者_运维知识库ubject = "SomeSubject";
string Body = LabelSendGridAbove.Text + sb.ToString() + LabelSendGridBetween.Text + "<br /><img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >" + LabelSendGridUnder.Text + "<BR>";
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);
}
}
}
}
}
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.
Thank you.
If I understand correctly, two mails are sent but only to one address...the first address from the DB...am I correct?
If so, then the problem line is
SqlCommand emailAdres = new SqlCommand("SELECT tblUsers.tUserEmail FROM tblUsers", sqlConn3);
Seems to me that you are calling the same email address on each iteration of the for...each loop. Try adding a where clause and a search parameter.
精彩评论