从网站发送bul电子邮件 [英] sending bul email from site

查看:97
本文介绍了从网站发送bul电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP> NET和c#



我有一个名为EmailData的数据库,其中有三列ID,UserName和Email,它们是所有usera的地址集合我的网站。

我尝试向网站上的所有用户发送电子邮件。



当我尝试发送给所有用户时,电子邮件仅发送给第一个用户,而不是所有用户。



我使用以下代码:



I am using ASP>NET with c#

I have a database named EmailData with three columns ID, UserName and Email which is collection of addresses of all usera of my site.
I am tryig to send an email to all the users from the site.

When i try to send to all the users, the email goes only to the first user, not to all users.

I am using the following code:

protected void SendEmailButton_Click(object sender, EventArgs e)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["EmailsConnectionString"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            
            SmtpClient client = new SmtpClient("mail.mysite.com", 25);
            MailMessage mail = new MailMessage();

            mail.From = new MailAddress("info@mysite.com", "MY SITE");
            mail.To.Add(EmailTextBox.Text);

            SqlCommand command = new SqlCommand("SELECT Count(*) from EmailData", connection);
            int count = int.Parse(command.ExecuteScalar().ToString());

            for (int i = 0; i <= count; i++)
            {
                SqlCommand com1 = new SqlCommand("SELECT Email FROM EmailData", connection);
                string email = com1.ExecuteScalar().ToString();
                mail.Bcc.Add(email);
            }
            mail.Subject = SubjectTextBox.Text;

            mail.IsBodyHtml = true;
            mail.Body = MessageTextBox.Text;

            NetworkCredential mycredentials = new NetworkCredential("message@mysite.com", "mypassword");
            client.UseDefaultCredentials = false;
            client.Credentials = mycredentials;

            client.Send(mail);
            connection.Close();
        }
    }





我一直无法弄清楚我的循环有什么问题使用该电子邮件只发给一个用户。



请帮助我。

非常感谢你的帮助。



I have not been able to figure out what is wrong with the loop that i am using that email goes only to one user.

Kindly help me.
Many thanks for your help.

推荐答案

参考 - SqlCommand.ExecuteScalar Method [ ^ ]。

Refer - SqlCommand.ExecuteScalar Method[^].
Quote:

执行查询,并返回查询返回的结果集中第一行的第一列。其他列或行将被忽略。

Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

因此,它无法返回所有行。



您必须使用SqlCommand.ExecuteReader方法 [ ^ ]或 DataAdapter.Fill Method(DataSet) [ ^ ]。然后遍历结果并添加到 BCC 。您不必使用 For Loop



请参阅使用DataReader检索数据 [ ^ ]。

So, it can't return all the Rows.

You have to use SqlCommand.ExecuteReader Method[^] or DataAdapter.Fill Method (DataSet)[^]. Then loop through the results and add to BCC. No need to use For Loop as you have used.

See example at Retrieving Data Using a DataReader[^].


问题是因为使用 com1.ExecuteScalar()。for循环中的ToString()返回结果集中第一行的第一列。因此循环中的每次迭代都从查询中获取相同的值。



MSDN说



The issue is because of using com1.ExecuteScalar().ToString() inside the for loop which returns returns the first column of the first row in the result set.So each iteration in the loop fetches the same value from the query.

MSDN says

引用:

执行查询,并返回查询返回的结果集中第一行的第一列。其他列或行将被忽略。

Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.





因此,使用DataReader代替ExecuteScalar方法,以便迭代从查询和发送电子邮件返回的每封电子邮件。 br $>


DataReader [ ^ ]


SqlCommand com1 = new SqlCommand(SELECT Email FROM EmailData, connection);
               string email = com1.ExecuteScalar().ToString();
               mail.Bcc.Add(email);





此代码始终返回第一行。除了执行查询外,您必须将其置于循环中。





This code always return the first rows.You have to put it in loop except the execute query.

using (SqlDataReader dr = cmd.ExecuteReader())
  {
      while (dr.Read())
      {
          msg.CC.Add((string)dr[&quot;user_email&quot;]);
      }
  }


这篇关于从网站发送bul电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆