0

我正在尝试将批量邮件列表添加到地址,以便我可以一次发送该批量邮件列表。在这里,我通过循环进入从我的数据集返回的每条记录来完成此操作。有没有更好的方法来添加该总数据集到没有循环的地址列表。提前谢谢。这是我的代码

 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);
4

1 回答 1

0

您可以删除一个循环,然后执行以下操作:

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);
}
于 2016-04-18T12:30:15.290 回答