title: C# Sending an email using a list for addresses
body: <p>just had a quick question for you. I made a small application some-time ago which ends off by sending out an email to 4 recipients. However, I hard-coded the addresses. Going back to change things, I'd like to put the addresses in a .csv file and I will just read it in. This way, I can edit the .csv to control the email addresses.</p>
  
  <p>I am just unsure on how you set up the recipients in the code. Obviously the variable name for the MailAddress has to be different, so I don't really know how to set up the loop, or what the proper way to do it would be.</p>
  
  <p>Below is my code that I used, and commented out is my new code which reads in all of the email addresses from the file into a list. Can you point me in the right direction of how I'd go from there to send the email to all of the addresses in the list?</p>
  
  <p>Thank you, </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.IO;
  using System.Net;
  using System.Net.Mail;
  using System.Net.Mime;
  using System.Data;
  using Microsoft.Office.Interop;
  using Microsoft.Office.Interop.Excel;
  using Microsoft.Office.Core;
  using System.Reflection;
  using System.Drawing;
  using System.Globalization;
  
  namespace ReportEdit
  {
      class sendEmail
      {
          public static void SendMyMail()
          {
  
  
             /* !!NEW!! READING IN THE .CSV TO GET LIST OF ADDRESSES. Below comment is old 
  
              List&lt;string&gt; addyList = new List&lt;string&gt;();
              foreach (string line in File.ReadLines(Properties.Settings.Default.AddyList))
              {
                  addyList.Add(line);
              }
              */
  
              SmtpClient companySmtpClient = new SmtpClient("smtprelay.company.com");
  
              companySmtpClient.UseDefaultCredentials = true;
  
              MailAddress from = new MailAddress("ActiveBatchRunReport@company.com", "ActiveBatchRunReport");
              MailAddress to = new MailAddress("GrpDstISOne@company.com", "DstOne");
              MailAddress Recipient1 = new MailAddress("Bill@company.com", "Bill");
              MailAddress Recipient2 = new MailAddress("Tom@company.com", "Tom");
              MailAddress Recipient3 = new MailAddress("Gena@company.com", "Gena");
              MailAddress Recipient4 = new MailAddress("Clifford@company.com", "Clifford");
              MailAddress ccKate = new MailAddress("Kate@company.com", "Kate");
  
  
              MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
  
              myMail.To.Add(Recipient1);
              myMail.To.Add(Recipient2);
              myMail.To.Add(Recipient3);
              myMail.To.Add(Recipient4);
  
              myMail.CC.Add(ccKate);
  
              myMail.Subject = "Daily Job Runs";
              myMail.SubjectEncoding = System.Text.Encoding.UTF8;
  
              myMail.Body = "Attached you will find an Excel spreadsheet" +
              "Total Job counts are listed at the bottom.";
  
              myMail.BodyEncoding = System.Text.Encoding.UTF8;
              myMail.IsBodyHtml = true;
  
              myMail.Attachments.Add(new Attachment(@"PathToAttachment"));
  
              companySmtpClient.Send(myMail);
          }
  
      }
  }
  </code></pre>