如何使用 SqlDataReader 向 table 中的每个人发送电子邮件

How to send email to everyone in the table with using SqlDataReader

我正在使用代码向 table 中的每个人发送电子邮件 { table_name(id, email, username) } 但是此代码仅将邮件发送到 table.

中的第一封电子邮件
public void Sendmail()
        {
            SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
            con1.Open();
            SqlCommand cmd1 = new SqlCommand("select * from table_name", con1);
            SqlDataReader dr = cmd1.ExecuteReader();
            if (dr.Read())
            {
                string useremail = dr["email"].ToString(); //i assume this is the problem not sure
                string username = dr["username"].ToString(); //i assume this is the problem not sure
                var mm = new MailMessage
                {
                    From = new MailAddress("testerforeverything@gmail.com", "bot"),
                    Subject = sub.Text,
                    Body = string.Format("hello,Subscriber<br/>{0}<h3>This is bot<h3>", body.Text),
                    IsBodyHtml = true
                };
                while (dr.Read())
                {
                    var to = new MailAddress(useremail, username);
                    mm.To.Add(to);
                }
                //MailMessage mm = new MailMessage("testerforeverything@gmail.com",useremail)
                //{
                //    Subject = sub.Text,
                //    Body = string.Format("hello,Subscriber<br/>{0}<h3>This is bot.<h3>", body.Text),
                //    IsBodyHtml = true
                //};
                //var datatable = new DataTable();
                //var numberofrows = datatable.Rows.Count;
                //for (int i = 0; i < numberofrows; i++)
                //{
                //    useremail = (string)datatable.Rows[i]["email"];
                //    mm.To.Add(new MailAddress(useremail));
                //}
                SmtpClient smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    EnableSsl = true
                };
                NetworkCredential nc = new NetworkCredential
                {
                    UserName = "testerforeverything@gmail.com",
                    Password = "password"
                };
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = nc;
                smtp.Port = 587;
                smtp.Send(mm);
                con1.Close();
                Response.Redirect(Request.RawUrl);
            }
        }

和之前我用的是注释代码,没什么区别,请帮忙。 谢谢。

  1. 您使用的 SqlDataReader 有误。请阅读 this post.
  2. 您应该将 SmtpClient 对象移出循环。
  3. Response.Redirect(Request.RawUrl);应该在循环之后。

这是固定代码。

 public void Sendmail()
    {
        SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
        con1.Open();
        SqlCommand cmd1 = new SqlCommand("select * from table_name", con1);
        SqlDataReader dr = cmd1.ExecuteReader();

        SmtpClient smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                EnableSsl = true
            };
            NetworkCredential nc = new NetworkCredential
            {
                UserName = "testerforeverything@gmail.com",
                Password = "password"
            };
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = nc;
            smtp.Port = 587;

        while (dr.Read())
        {
            string useremail = dr["email"].ToString(); //i assume this is the problem not sure
            string username = dr["username"].ToString(); //i assume this is the problem not sure
            var mm = new MailMessage
            {
                From = new MailAddress("testerforeverything01@gmail.com", "bot"),
                Subject = sub.Text,
                Body = string.Format("hello,Subscriber<br/>{0}<h3>Team makes IELTS simple<h3>", body.Text),
                IsBodyHtml = true
            };

            var to = new MailAddress(useremail, username);
            mm.To.Add(to);

            smtp.Send(mm);                
        }
        con1.Close();
        Response.Redirect(Request.RawUrl);
    }