title: ASP.NET Core 3 Passing List of Emails to Send Method Mailkit
body: <p>I'm using MailKit to send emails for account confirmation and password reset.  These operations work as intended, but now I'm trying to modify the same code to also send email notifications to all registered users but this is where I'm having issues.</p>
  
  <p>I've built a List collection using the following code:</p>
  
  <pre><code>public List&lt;string&gt; UserList {
      get {
          var allUsers = userManager.Users;
          return allUsers.Select(x =&gt; x.Email).ToList();
      }
  }
  </code></pre>
  
  <p>this collection returns all emails within my AspNetUsers table successfully.</p>
  
  <p>Next I create 3 variables:</p>
  
  <pre><code>var toAddress = UserList;
  var subject = "New Asset Added" + " " + model.Name;
  var body = model.Name + model.AssetType + model.AssetURL;
  </code></pre>
  
  <p>When debugging and stepping through the code I do see <code>Count = 2</code> and then the list of emails in the variable.  So far this looks good</p>
  
  <p>After the 3 variables I have this line of code that passes the collected information to the Send method:</p>
  
  <pre><code>_emailSender.Send(toAddress[0], subject, body);
  </code></pre>
  
  <p>Again, the <code>toAddress[0]</code> when stepping through code shows <code>Count = 2</code> so I'm assuming the way I have it written is valid.</p>
  
  <p>Once I reach the Send Method code and I check the <code>toAddress</code> variable again, it is now stripped of the <code>Count = 2</code> and becomes just the first email address in the list which makes sense since I passed the variable with an index of 0.  What I really need is to pass all emails not just 1.</p>
  
  <p>If I remove the brackets or leave the brackets empty I get an error:</p>
  
  <pre><code>cannot convert from 'System.Collections.Generic.List&lt;string&gt;' to 'string'
  </code></pre>
  
  <p>So for now I'm forced to add an index.</p>
  
  <p>My send method seems fairly simple but I do believe my foreach loop might also not be well formulated.  Here's my Send Method in it's entirety.</p>
  
  <pre><code>public async void Send(string toAddress, string subject, string body, bool sendAsync = true) {
      var mimeMessage = new MimeMessage(); // MIME : Multipurpose Internet Mail Extension
      mimeMessage.From.Add(new MailboxAddress(_fromAddressTitle, _fromAddress));
  
      foreach (char toAddresses in toAddress) {
          mimeMessage.To.Add(new MailboxAddress(toAddresses)); //I get error saying cannot convert char to string.  
      }
  
      mimeMessage.Subject = subject;
  
      var bodyBuilder = new MimeKit.BodyBuilder {
          HtmlBody = body
      };
  
      mimeMessage.Body = bodyBuilder.ToMessageBody();
  
      using (var client = new MailKit.Net.Smtp.SmtpClient()) {
          client.Connect(_smtpServer, _smtpPort, _enableSsl);
  
          client.Authenticate(_username, _password); // If using GMail this requires turning on LessSecureApps : https://myaccount.google.com/lesssecureapps
          if (sendAsync) {
              await client.SendAsync(mimeMessage);
          }
          else {
              client.Send(mimeMessage);
          }
  
          client.Disconnect(true);
      }
  }
  </code></pre>
  
  <p>My end goal is to use a foreach loop on the  <code>mimeMessage.To.Add(new MailboxAddress(toAddresses));</code> so that everyone can receive an email if there is more than 1 email address, if not it can loop once.</p>
  
  <p>Thanks in advance!</p>