如何使用sparkpost将电子邮件批量列表添加到从数据集返回的地址中



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

 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.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);
}

相关内容

  • 没有找到相关文章

最新更新