Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
client.Send(message);
public string SndUpdateNotificationEmail(string updateID, string Duedate)
    {
        // updateID = UpdateUserID(updateID);//  where(Update_UserID = '" + updateID + "')

        string sql = "select * from Cwipuser.CWIP_Variances";
        SqlCommand cmd = new SqlCommand(sql, MyCon);
        MyCon.Open();
        SqlDataReader rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
            if (!rdr.IsDBNull(1))
            {
                updateID = rdr.GetString(1);
            }

            MailMessage message = new MailMessage();
            string MessageFrom = "SSCReportAndAnalysis@telkom.co.za";
            message.From = new MailAddress(MessageFrom);
            string MessageTo = updateID + "@telkom.co.za";
            message.To.Add(MessageTo);
          
          try
          {
              client.Dispose();
          }
          catch
          {
              //AlertString.Alert(ref _page, "Mail not sent", "key");
          }
      }
      MyCon.Close();
      return updateID;
  }
Posted
Updated 1-Sep-14 0:11am
v2
Comments
[no name] 1-Sep-14 6:12am    
Did you maybe forget to ask some sort of a question or describe an actual problem?
mampaf 1-Sep-14 6:54am    
ooh sorry.see what happens is the coding works well but it sends an email twice to the user at index(1),like it starts sending an email to indx(1) then goes to 0,1 again then,2,3,4,5,6,7,8,9 and executes.
the problem is that i want it to send all users one email.
[no name] 1-Sep-14 8:25am    
According to your code you are not sending anything to anyone. If it's sending twice to the same person, it's probably because he is in your database twice. Why are you selecting everything there is to get out of your database when all you use is one field? Doesn't that seem kind of wasteful to you? Why are you not adding all of the recipients to your message's To field if that is what you want to do?
mampaf 1-Sep-14 8:40am    
oooh ya i saw that but dont kno how to solve it, see i want to send email acording to the Dta in a VIEW not from a table,so is it possible to say something like "select * from View where ID = Id"??

Create your mail message before while loop.
Within the loop add to To or CC or BCC collections like this:

C#
MailMessage message = new MailMessage();
string MessageFrom = "SSCReportAndAnalysis@telkom.co.za";
message.From = new MailAddress(MessageFrom);
string MessageTo = string.empty;

while (rdr.Read())
{
if (!rdr.IsDBNull(1))
{
updateID = rdr.GetString(1);
message.To.Add(new MailAddresss(updateID + "@telkom.co.za"));
/*
the above line can be 
message.CC.Add(new MailAddresss(updateID + "@telkom.co.za"));
OR 
message.BCC.Add(new MailAddresss(updateID + "@telkom.co.za"));
*/
}
 
Share this answer
 
Comments
mampaf 1-Sep-14 6:48am    
it gives me this error "The parameter 'addresses' cannot be an empty string.
Parameter name: addresses"
Sinisa Hajnal 1-Sep-14 13:47pm    
which line?

Did you try using CC or BCC?
Try setting only message.To = New MailAddress(...). Play with it for a bit.

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