Greg
Greg

Reputation: 746

Sending an email with PasswordRecovery when there are multiple accounts

I'm using the PasswordRecovery Control and can't send more then one email when there are multiple accounts with the same email. I get a MembershipUserCollection with Membership.FindUsersByEmail. I then loop through it in a foreach. My problem is if there is more then one user it only sends the last email. How can I get it to send an email for each account as it loops through? The delagate is called the correct number of times. Also, I know they are all going to the same email, but would like there to be one sent for each account.

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

Upvotes: 2

Views: 1191

Answers (1)

Greg
Greg

Reputation: 746

Figured it out ... the way I was originally doing it would not work, so I went semi-custom. I added an event handler on the submit button and edited the code as shown below. As you can see, I simply looped thorough the collection. Not the best I'm sure, but it works and is easy to understand.

The body of the email is created in a txt file with html formatting. Using the mailDefinition class allows me to have replacement strings, which simplifies the emails body creation.

It sends a separate email for each account to the same email. I could have put them all into a single email, but this is what they wanted ...

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

Upvotes: 1

Related Questions