在gridview的复选框中发送多个电子邮件不工作
本文关键字:电子邮件 工作 gridview 复选框 | 更新日期: 2023-09-27 17:50:11
我希望有人帮助解决以下问题。我正试图向在gridview中检查的用户发送电子邮件。我四处搜索,发现了许多看起来相似的主题,但我找不到一个有效的解决方案。我知道我遗漏了一些东西,可能在我的select语句中。但是我想不明白。
我的代码显示了一个gridview,假设有2个用户,许多项目和来自用户的电子邮件地址。我为每个用户选择了1个项目,所以应该发送2封电子邮件。发送了2封邮件,但只发送到1个电子邮件地址,即他在数据库中首先遇到的电子邮件地址。我试着搜索userID,但不能使工作,我仍然在我的头几个月在开发。net。
查看我的代码:
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.Subject = "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);
}
}
}
}
}
我希望这段代码中没有太多错误。但如果有人知道我应该添加到代码,使其工作,所以它会发送2个电子邮件到2个不同的电子邮件地址,这将是非常好的。谢谢。
如果我理解正确的话,两封邮件被发送到一个地址…数据库中的第一个地址…我说的对吗?
如果是,那么问题行是
SqlCommand emailAdres = new SqlCommand("SELECT tblUsers.tUserEmail FROM tblUsers", sqlConn3);
在我看来,你在每次迭代中调用相同的电子邮件地址…每一个循环。尝试添加where子句和搜索参数。