Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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.
Posted
Updated 2-Feb-14 6:34am
v2
Comments
Richard MacCutchan 2-Feb-14 12:37pm    
A bit of debugging should quickly show you what the value is of count, and what names get added to the Bcc field.
Member 10235977 2-Feb-14 15:25pm    
I tried to get the value of count and BCC by adding a label. Count gives the totla number of records but the email simply repeats the first email address. the number of time it is repeated is eqaul to number of records. The output in my case was count = 150 and email label that i added haad the value like a@b.ca@b.ca@b.c and it is rep[eated 150 times if the first email address is a@b.c
JoCodes 2-Feb-14 12:38pm    
Whats returned from "SELECT Email FROM EmailData"
Member 10235977 2-Feb-14 15:13pm    
Email Address
Krunal Rohit 2-Feb-14 12:44pm    
Just put a Breakpoint and see what is the value of count ?

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.
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[^].
 
Share this answer
 
Comments
JoCodes 3-Feb-14 0:52am    
I was bit too late , Tadit :)
:D Ha ha. :)
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

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.


So instead of ExecuteScalar method use DataReader so it iterates a through each email returned from the Query and Sends email.

DataReader[^]
 
Share this answer
 
v2
C#
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.

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900