Unfortunately, since each email is personalized, I see no other way than a loop. So just change your method to something like:
public virtual MailMessage Welcome(string email, string name) { var mailMessage = new MailMessage{Subject = "Welcome to MvcMailer"}; mailMessage.To.Add(email); ViewBag.Name = name; PopulateBody(mailMessage, viewName: "Welcome"); return mailMessage; }
And then call this method inside your loop and send it at the same time.
Important Note
You must configure your web.config to use the pickup directory, not the SMTP server. Then start IIS to send an email from the pickup directory.
Reasoning. Since you can potentially call SmtpClient.Send(MailMessage mailmessage)
any number of times, this can become quite expensive if you need to connect to the SMTP server each time to send e-mail.
A good side effect of this is that you also get some redundancy if the SMTP server is unavailable or unavailable for any reason.
source share