c# - 使用 Mvc Mailer 向多个地址发送电子邮件

标签 c# asp.net-mvc email mvcmailer

我有一个 emails 变量,其中包含角色中用户的电子邮件地址列表。该列表不是以逗号分隔的。这是我目前拥有的代码:

    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));
        });
    }

我收到一条错误消息,指出'string'不包含'Email'的定义...

我应该使用什么来代替 x.To.Add(user.Email)

更新#1

我可以向预期收件人发送电子邮件,但邮件正文为空。消息正文已在 View 中构建。在我使用下面答案中的代码之前它就可以工作,我怎样才能让它再次工作?

最佳答案

这就是我重写的方式:

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;
}

希望对您有所帮助。

关于c# - 使用 Mvc Mailer 向多个地址发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20825197/

相关文章:

c# - 避免在 switch 语句中进行硬编码

asp.net-mvc - WebSecurity.InitializeDatabaseConnection 方法只能调用一次

visual-studio - 以编程方式配置 Outlook 邮件设置?

c# - 使用 Task.Factory 清理电子邮件异步

PHPMailer 无效地址

php - 将电子邮件重定向到网址

c# - Protobuf-net 序列化/反序列化到/从字节数组

c# - 如何自动增加包版本号?

c# - 将原始字节数据转换为 float[]

asp.net-mvc - IIS 8.5 - 应用程序初始化不起作用