0

我正在尝试控制电子邮件地址是否已经在列表中,如果是这样,我想将网格视图中的一行添加到将发送到该电子邮件帐户的新网格视图中。

我有一些代码来获取行并将它们发送到电子邮件地址。但问题在于,即使检查了该地址的多行,它也只向一个电子邮件地址发送 1 行。

在这里查看我的代码:

protected void ButtonATH_Click(object sender, EventArgs e)
    {
        List<string> lst = new List<string>();
        for (int i = 0; i < GridViewBestelling.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GridViewBestelling.Rows[i].Cells[0].FindControl("CheckBoxAfTeHalen");
            Label bestelID = (Label)GridViewBestelling.Rows[i].Cells[5].FindControl("LabelBestelID");
            Label lblUsrE = (Label)GridViewBestelling.Rows[i].Cells[7].FindControl("LabelEmailGeb");
            string emadr = lblUsrE.Text.ToString();
            string conn = "Data Source=pc-...";

            GridView grd = new GridView();

         if (ck.Checked)
            {
                System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(conn);
                sqlConn.Open();
                System.Data.SqlClient.SqlCommand updateCommand = new System.Data.SqlClient.SqlCommand("UPDATE tblBestelling SET tBestAfTeHalen = '" + ck.Checked + "' WHERE tBestId= '" + bestelID.Text + "'", sqlConn);
                updateCommand.Parameters.AddWithValue("@bestID", bestelID.Text);
                updateCommand.ExecuteNonQuery();

                LabelSendGridBoven.Text = "<b>Hello: <br /><br /> ";
                LabelSendGridBoven.Visible = false;

                LabelHoeveelheid.Text = "<br /><br /> Amount" ;
                LabelHoeveelheid.Visible = false;

                LabelSendGridTussen.Text = "Regards";
                LabelSendGridTussen.Visible = false;

                LabelSendGridOnder.Text = "<br /><br />----------------------" ;
                LabelSendGridOnder.Visible = false;

                DataTable dt = new DataTable();
                DataRow dr;

                dt.Columns.Add(new DataColumn("B_ID"));
                dt.Columns.Add(new DataColumn("P"));
                dt.Columns.Add(new DataColumn("M"));
                dt.Columns.Add(new DataColumn("H"));

                dr = dt.NewRow();
                dr["B_ID"] = ((Label)GridViewBestelling.Rows[i].Cells[5].FindControl("LabelBestelID")).Text;
                dr["P"] = ((Label)GridViewBestelling.Rows[i].Cells[8].FindControl("LabelProduct_naam")).Text;
                dr["M"] = ((Label)GridViewBestelling.Rows[i].Cells[9].FindControl("LabelManufact_Nr")).Text;
                dr["H"] = ((Label)GridViewBestelling.Rows[i].Cells[3].FindControl("LabelHoeveelheid")).Text;
                dt.Rows.Add(dr);

                grd.DataSource = dt;
                grd.DataBind();

                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                grd.RenderControl(htw);

              if ( !lst.Contains(emadr))
              {
                  lst.Add(emadr);

                  System.Data.SqlClient.SqlConnection sqlConn2 = new System.Data.SqlClient.SqlConnection(conn);
                  sqlConn2.Open();
                  System.Data.SqlClient.SqlCommand updateCommand2 = new System.Data.SqlClient.SqlCommand("UPDATE tblBestelling SET Status=@statusChange WHERE tBestId= '" + bestelID.Text + "'", sqlConn2);
                  updateCommand2.Parameters.AddWithValue("@statusChange", statusChange.ToString());
                  updateCommand2.Parameters.AddWithValue("@bestID", bestelID.Text);
                  updateCommand2.ExecuteNonQuery();
                  sqlConn2.Close();

                    try
                    {
                        MailMessage mail = new MailMessage();
                        mail.To.Add(emadr.ToString());
                        mail.Bcc.Add("SomeoneBcc");
                        mail.From = new MailAddress("FromWho");
                        mail.Subject = "SomeSubject";
                        string Body = LabelSendGridBoven.Text + sb.ToString() + LabelHoeveelheid.Text + LabelSendGridTussen.Text + "<br /><img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >" + LabelSendGridOnder.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("..server..");
                        smtp.Send(mail);
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                }
            }
        }
    }

这会导致向每个电子邮件帐户发送一封电子邮件,该电子邮件已检查所有行,但我只想要与电子邮件地址对应的行。

请问有人有什么想法吗?

4

2 回答 2

0
lst.Contains(emadr).ToString() == emadr.ToString()

Welllst.Contains()返回一个布尔值,您将其转换为字符串,因此除非您的电子邮件看起来像true或者false它不起作用

为什么不直接使用:

if(lst.Contains(emadr))
{...}
于 2011-04-20T07:57:52.693 回答
0

我认为你在这里做错了什么

if (lst.Contains(emadr).ToString() == emadr.ToString())
  {...}

lst.Contains(emadr).ToString()为真或假,永远不会匹配成员emadr中的电子邮件地址

从所提到的评论中,您应该使用

if (lst.Contains(emadr))
{
//your logic
}
于 2011-04-20T08:02:37.857 回答