Customise Password Recovery Email

The majority of web applications need some sort of authentication component, and ASP.Net's authenication is excellent and allows you all the functionality you would expect.

On a recent web application we were required to provide users with the facility to recover their password, and as you'd expect there is a PasswordRecovery component that you just drop on a page and it works.

Unfortunately the emails that resulted from the control were not as rich as the client required. i.e HTML and Plain text alternate views, the HTML alternate view also used linked resources so the mail arrives in your mailbox with all its images attached. No need to download the images.

After a little bit of thought it was easy to override the PasswordRecovery mails using the SendingMail event.

protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{

    string pwd = Membership.GetUser(PasswordRecovery1.UserName).GetPassword(PasswordRecovery1.Answer);

    ProfileCommon UserProfile = Profile.GetProfile(PasswordRecovery1.UserName);
       
    //  send custom email at this point..  insert custom mail here
        
    // cancel the component's email here
    e.Cancel = true;

}

This simple code allows you to override the component's standard email and extract the user's password. With this information you can then insert your own custom mail object and send the recovery information to the user using a template of your choice...

Job done.. Hope this helps you out

Author Paul Marshall

A self confessed Microsoft bigot, Paul loves all Microsoft products with a particular fondness for SQL Server. Paul is currently focusing on Web 2.0 patterns and practices and is always looking for better ways of doing things.

I love the .net platform, and I find it to be the most productive toolset I have used to date.

Comments

Atip C said:

Thank You, it works great! Here is my code

.............................

        string pwd = Membership.GetUser(PasswordRecovery1.UserName).ResetPassword();


        ProfileCommon UserProfile = Profile.GetProfile(PasswordRecovery1.UserName);
        MailMessage mail = new MailMessage();
        mail.To.Add(ws_your_cust_email_address);
        mail.From = new MailAddress("your@email.com");
        mail.Subject = "Your New Password";
        string Body = "Dear Customer" + ",\r\n\r\nPlease return to the site and log in using the following information" + "\r\n\r\nYour Username: " + PasswordRecovery1.UserName.Trim() + "\r\n\r\nPassword: " + pwd + " \r\n\r\nThank you.";
        mail.Body = Body;
        //mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "XX.XXX.XXX.XXX"; //Or Your SMTP Server Address
        smtp.Port = 25;
        smtp.Send(mail);
        // cancel the component's email here
        //e.Cancel = true;  
02/Feb/2012 17:00 PM

Add Comment

Name
Comment
 

Your comment has been received and will be shown once it passes moderation.