我想帮助解决以下问题。我正在尝试向在gridview中签入的用户发送电子邮件。我四处搜寻,发现许多看似相似的主题,但我找不到一个有效的解决方案。嗯,我知道我错过了一些东西,也许在我的选择陈述中。但我无法弄明白。
我的代码显示了一个gridview,让我们说两个用户,很多项目以及用户的电子邮件地址。我为每个用户选择了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个不同的电子邮件地址,这将是非常好的。
谢谢。
答案 0 :(得分:1)
如果我理解正确,会发送两封邮件但只发送到一个地址......来自数据库的第一个地址......我是否正确?
如果是,那么问题行是
SqlCommand emailAdres = new SqlCommand("SELECT tblUsers.tUserEmail FROM tblUsers", sqlConn3);
对我而言,在for ... each循环的每次迭代中,您都在调用相同的电子邮件地址。尝试添加where子句和搜索参数。