2

我正在使用 PasswordRecovery Control,当有多个帐户使用同一个电子邮件时,我不能发送超过一封电子邮件。我通过 Membership.FindUsersByEmail 获得 MembershipUserCollection。然后我在 foreach 中循环遍历它。我的问题是,如果有多个用户,它只会发送最后一封电子邮件。当它循环通过时,我怎样才能让它为每个帐户发送一封电子邮件?delagate 被称为正确的次数。另外,我知道他们都去同一个电子邮件,但希望为每个帐户发送一个。

代码片段:

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

1 回答 1

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";
    }
}
于 2012-08-20T18:05:25.863 回答