Charles Frank

Charles Frank

  • NA
  • 16
  • 3k

Need to loop through names in table and send all an email

Mar 28 2016 9:11 PM

Hi All,
 
I was wondering if someone could help me. I've got a simple Suppliers table that has the Email Addresses for my Suppliers.
I need to loop through the email addresses 'SuppEmail' in the Suppliers table in the SQL Database 'SpecCars' and send them all the email below.
I've been at it for a few days now, looking on-line and trying many different variations, but no matter what I do, it only sends one email
to the first entry '[email protected]' in the table and that's it. I don't think my loop is correct, I'm putting the SuppEmail into
the variable emailnew, but it doesn't seem to work. If you could help, that would be fantastic. It's an ASP.NET C# Solution.
 
 // This is the Suppliers Table, it just has two record in there:
 
..................................................

use SpecCars
Go

CREATE table Suppliers(
 SuppId INT IDENTITY(1,1) PRIMARY KEY,
 SuppName NVARCHAR(60) NOT NULL, 
 SuppAddress NVARCHAR(150) NOT NULL,
 SuppSuburb NVARCHAR(60) NOT NULL, 
 SuppState NVARCHAR(30) NOT NULL,
 SuppPost NVARCHAR(10) NOT NULL,
 SuppPhone NVARCHAR(10) NOT NULL,
 SuppEmail NVARCHAR(100) NOT NULL,
 SuppCode NVARCHAR(10) NOT NULL
)
Go

Command(s) completed successfully.


Insert into Suppliers (SuppName, SuppAddress, SuppSuburb, SuppState, SuppPost, SuppPhone, SuppEmail, SuppCode) values ('Jacks Auto', '2 Jill Street', 'Belgrade', 'VIC', '3299', '9555 4457', '[email protected]', 'JACBLA')
Insert into Suppliers (SuppName, SuppAddress, SuppSuburb, SuppState, SuppPost, SuppPhone, SuppEmail, SuppCode) values ('Ultimate Lights', '205 Browns Road', 'Tullamarine', 'VIC', '3011', '9877 2255', '[email protected]', 'ULTTUL')

(2 row(s) affected)

..................................................
 
 
 //This is the code snippet :
 
      SqlDataReader sqlData;
      SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SpecCars;Integrated Security=True");

      connection.Open();
      sqlData = new SqlCommand("Select SuppEmail From Suppliers", connection).ExecuteReader();

      int count = sqlData.FieldCount;
      while (sqlData.Read())
      {
         for (int i = 0; i < count; i++)
        {
          string emailnew = sqlData[i].ToString();

            MailMessage mailMessage = new MailMessage();

            mailMessage.From = new MailAddress("myemail.com");
            mailMessage.To.Add("myemail.com");
            mailMessage.To.Add(emailnew);
            //mailMessage.CC.Add(emailnew);
            mailMessage.Subject = "Assembly Line Stop";
            mailMessage.Priority = MailPriority.High;

            mailMessage.Body = "Please be advised that the assembly line at Specialised Cars has STOPPED. You will be notified once the line has started again. Any Services between the LINE STOP and the LINE START will be carried out after 19:00 (7pm).";
            mailMessage.IsBodyHtml = true;

            SmtpClient smtpClient = new SmtpClient("smtp-mail.myprovider.com", 587);
            smtpClient.EnableSsl = true;
            smtpClient.Credentials = new System.Net.NetworkCredential("myemail.com", "password");
            smtpClient.Send(mailMessage);
        }
      }
        connection.Close();
  
..................................................
 


Answers (15)