I have an emails
variable that contains a list of e-mail addresses of users in a role. The list is not comma-separated. Here's the code that I have at the moment:
public virtual MvcMailMessage AdminNotification()
{
var userRoles = Roles.GetUsersInRole("Admin");
var users = new List<MembershipUser>();
var emails = new List<String>();
foreach (var UserName in userRoles)
{
var user = Membership.GetUser(UserName, false);
users.Add(user);
emails.Add(user.Email);
}
return Populate(x =>
{
x.Subject = "AdminNotification";
x.ViewName = "AdminNotification";
emails.ForEach(user => x.To.Add(user.Email));
});
}
I get an error stating 'string' does not contain a definition for 'Email'...
What should I use instead of x.To.Add(user.Email)
?
Update #1
I'm able to send out e-mails to the intended recipients, but the message body is blank. The message body has been constructed in a View. It was working before I used the code in the answer below, how can I get it working again?
This is how I'd rewrite it:
public virtual MvcMailMessage AdminNotification()
{
string[] userRoles = Roles.GetUsersInRole("Admin");
IEnumerable<string> emails =
userRoles.Select(userName => Membership.GetUser(userName, false))
.Select(user => user.Email);
var message = new MvcMailMessage {Subject = "AdminNotification", ViewName = "AdminNotification"};
foreach (string email in emails)
{
message.To.Add(email);
}
return message;
}
I hope it helps.