title: How to list multiple pieces of data from data table using a variable
body: <p>I wrote the below program to send HTML Email to the demo users in this table:</p>
  
  <p><img src="https://i.stack.imgur.com/qEpf5.png" alt="enter image description here"></p>
  
  <p>I want to send only one email and lists all of the information for each customer. Currently, if a customer has 10 books past due, then the customer will get 10 emails based on the table structure, because each book loan is a separate record.</p>
  
  <p><strong>For example:</strong></p>
  
  <p>Dear Jim Carey:
  The following book(s) are due: </p>
  
  <ol>
  <li>How to Fix Cars</li>
  <li>James Bond Returns</li>
  </ol>
  
  <p>Is there a way that I can send only one email for each customer but list each item in the email. For example list all the books due?</p>
  
  <p>Fox example: Sending an email using the above table, Jim Carey will get 2 emails, instead of 1. I would like to send just one but list both the books due.</p>
  
  <pre><code>class Program
  {
  static DataSet dtProfile = Database.AcquireData();
  static DataTable table = dtProfile.Tables[0];
  
  static string CustFName;
  static string CustLName;
  static string CheckoutDate;
  static string DueDate;
  static string BookName;
  
  public static void SendEmail()
  {
      foreach (DataRow row in table.Rows)
      {
       CustFName = row["CustFName"].ToString();
       CustLName = row["CustLName"].ToString();
       CheckoutDate = row["CheckoutDate"].ToString();
       DueDate = row["DueDate"].ToString();
       BookName = row["BookName"].ToString();          
       string body = PopulateBody(CustFName, CustLName, CheckoutDate, DueDate, BookName);&lt;br /&gt;
       SendHtmlFormattedEmail("Email", "Subject", body);
      }
  }
  
  public static string PopulateBody(string custFName, string custLName,
      string checkoutDate, string dueDate, string bookName)
  {
      string body = string.Empty;
  
      using (StreamReader reader = 
          new StreamReader(Path.GetFullPath(@"Z:\folder\email.html")))
      {
          body = reader.ReadToEnd();
      }
      body = body.Replace("{#First Name#}", custFName);
      body = body.Replace("{#Last Name#}",  custLName);
      body = body.Replace("{#Checkout Date#}", checkoutDate);
      body = body.Replace("{#Due Date#}",  dueDate);
      body = body.Replace("{#Book Name#}", bookName);
  
      return body;
  }
  
  public static void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
  {
      using (MailMessage mailMessage = new MailMessage())
      {
        mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
        mailMessage.Subject = subject;
        mailMessage.Body = body;
        mailMessage.IsBodyHtml = true;
        mailMessage.To.Add(new MailAddress(recepientEmail));
        SmtpClient smtp = new SmtpClient();
        smtp.Host = ConfigurationManager.AppSettings["Host"];
        smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
        NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
        NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
        smtp.Send(mailMessage);
      }
  }
  
  static void Main(string[] args)
  {
      SendEmail();
  }
  
  }
  </code></pre>