当有多个帐户时,使用密码恢复功能发送电子邮件

时间:2012-08-10 16:16:04

标签: c# asp.net-membership password-recovery

我正在使用PasswordRecovery控件,如果有多个帐户使用相同的电子邮件,则无法发送多封电子邮件。我使用Membership.FindUsersByEmail获得MembershipUserCollection。然后我在foreach中循环它。我的问题是,如果有多个用户,它只发送最后一封电子邮件。如何在每个帐户循环播放时为其发送电子邮件? delagate被称为正确的次数。此外,我知道他们都将使用相同的电子邮件,但希望每个帐户都有一个。

Code Snip:

protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{

}

bool IsValidEmail(string strIn)
{
    // Return true if strIn is in valid e-mail format.
    return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}


protected void PasswordRecovery1_VerifyingUser(object sender, LoginCancelEventArgs e)
{
    if (IsValidEmail(PasswordRecovery1.UserName))
    {
        // string uName = Membership.GetUserNameByEmail(PasswordRecovery1.UserName) ?? PasswordRecovery1.UserName;
        MembershipUserCollection users = Membership.FindUsersByEmail(PasswordRecovery1.UserName);
        if (users.Count < 1)
        {
            PasswordRecovery1.UserName = " ";
            PasswordRecovery1.UserNameFailureText = "That user is not available"; }
        else
        {
            foreach (MembershipUser user in users)
            {

                PasswordRecovery1.UserName = user.UserName;
                PasswordRecovery1.SendingMail += PasswordRecovery1_SendingMail;
                PasswordRecovery1.SuccessTemplateContainer.Visible = true;

            }

        }
    }
    else
    { 
        PasswordRecovery1.UserName = " ";
        PasswordRecovery1.UserNameFailureText ="Please enter a valid e-mail";           
    }
}

1 个答案:

答案 0 :(得分:1)

想出来......我原来这样做的方式不行,所以我去了半定制。我在提交按钮上添加了一个事件处理程序,并编辑了如下所示的代码。正如你所看到的,我简单地完成了整个系列的循环。不是最好的我确定,但它的工作原理很容易理解。

电子邮件的正文是在带有html格式的txt文件中创建的。使用 mailDefinition 类允许我使用替换字符串,这简化了电子邮件正文创建。

它会将每个帐户的单独电子邮件发送到同一封电子邮件。我可以将它们全部放入一封电子邮件中,但这就是他们想要的......

protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{
    e.Cancel = true;
}

bool IsValidEmail(string strIn)
{
    // Return true if strIn is a valid e-mail
    return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}

protected void SubmitLinkButton_Click(object sender, EventArgs e)
{
    if (IsValidEmail(PasswordRecovery1.UserName))
    {
        // Get user collection by shared email
        MembershipUserCollection users = Membership.FindUsersByEmail(PasswordRecovery1.UserName);
        if (users.Count < 1)
        {
            PasswordRecovery1.UserName = " ";
            PasswordRecovery1.UserNameFailureText = "That user is not available";
        }
        else
        {
            // Loop and email each user in collection
            foreach (MembershipUser user in users)
            {
                MembershipUser ur = Membership.GetUser(user.UserName);

                DateTime now = DateTime.Now;

                // Using MailDefinition instead of MailMessage so we can substitue strings
                MailDefinition md = new MailDefinition();

                // list of strings in password.txt file to be replace
                ListDictionary replacements = new ListDictionary();
                replacements.Add("<%UserName%>", ur.UserName);
                replacements.Add("<%Password%>", ur.GetPassword());

                // Text file that is in html format
                md.BodyFileName = "absolute path to password.txt";
                md.IsBodyHtml = true;
                md.Priority = MailPriority.High;
                md.Subject = "Email Subject Line - " + now.ToString("MM/dd - h:mm tt");
                md.From = ConfigurationManager.AppSettings["FromEmailAddress"];

                // Add MailDefinition to the MailMessage
                MailMessage mailMessage = md.CreateMailMessage(ur.Email, replacements, this);
                mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["FromEmailAddress"], "Friendly Name");
                SmtpClient m = new SmtpClient();
                m.Host = "127.0.0.1";
                m.Send(mailMessage);

                PasswordRecovery1.UserName = user.UserName;

                PasswordRecovery1.SendingMail += PasswordRecovery1_SendingMail;
            }

        }
    }
    else
    {
        PasswordRecovery1.UserName = " ";
        PasswordRecovery1.UserNameFailureText = "Please enter a valid e-mail";
    }
}