问题描述
||
我需要以下问题的帮助。我正在尝试向在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);
在我看来,您在for ... each循环的每次迭代中都在调用相同的电子邮件地址。尝试添加where子句和搜索参数。