0

I am trying to add bulk mail list to addresses so that I can send that bulk mail list at a time.Here I have done this by looping into every record that returned from my dataset.Is there any better way to add that total dataset to the address list without looping.Thanks in advance.Here is my code

 DataSet ds = new DataSet();

 List<string> to = new List<string>();

//I have returned one dataset containing list of emails to be send.

 ds = email.GetBulkProcessMails("Process");

//here I am looping through every record in the dataset and adding that records to my List

 if (ds.Tables.Count > 0)
            {

                foreach (DataRow dtrow in ds.Tables[0].Rows)
                {
                    tomailscount bmscount = new tomailscount();
                    bmscount.destinationmails = dtrow["tomail_id"].ToString();
                    to.Add(bmscount.destinationmails.ToString());
                }
            }


//Here I am assigning that entire list to address and adding that recipient for transmission
  for (int i = 0; i < to.Count; i++)
            {
                var recipient = new Recipient
                {

                    Address = new Address { Email = to[i] }

                };
                transmission.Recipients.Add(recipient);
            }


//Here I am sending that transmission

var sparky = new Client(ConfigurationManager.AppSettings["APIKey"]);
sparky.Transmissions.Send(transmission);
  • If `ds.Tables[0].Rows` is constant you could put it in a "Recipient List" and send via list ID. This would allow you to separate your "Recipient List" management from your transmissions. – Yepher Apr 26 '16 at 15:19

1 Answers1

0

You can remove one loop, and just do this:

if (ds.Tables.Count > 0)
{

    foreach (DataRow dtrow in ds.Tables[0].Rows)
    {
        tomailscount bmscount = new tomailscount();
        bmscount.destinationmails = dtrow["tomail_id"].ToString();
        to.Add(bmscount.destinationmails.ToString());
        transmission.Recipients.Add(new Recipient {
          Address = new Address { Email = bmscount.destinationmails.ToString() }
        });
    }

    // send it
    var sparky = new Client(ConfigurationManager.AppSettings["APIKey"]);
    sparky.Transmissions.Send(transmission);
}
mberacochea
  • 1,716
  • 1
  • 12
  • 18