Link to home
Start Free TrialLog in
Avatar of endrec
endrec

asked on

How to customize the e-mail sent by PasswordRecovery In The SendingMail Event and Include The Newly Reset Password In The E-mail?

Hi,
I have a multi-lingual application that needs to send different password recovery e-mail messages to the user based on their language preference.  The problem is that I cannot simply read one .txt file from the MailDefinition of the PasswordRecovery control due to this and when I try to programatically customize the e-mail sent by the system the <% Username %> and <% Password %> tokens are not being replaced.

How can I customize the e-mail message sent to the user when they are resetting their password and include the new password in the e-mail?

Please note that I get an error when I manually try to reset the password in the SendingMail event, but I could not find another way to obtain a reset password for the e-mail.
Protected Sub PasswordRecovery1_SendingMail(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MailMessageEventArgs) Handles PasswordRecovery1.SendingMail
        Dim strFinalBody As String = TryCast(GetGlobalResourceObject("Emails", "PasswordResetBody"), String)
 
        Try
            Dim usr As MembershipUser = Membership.GetUser(PasswordRecovery1.UserName)
            'Returns object not set to an instance of an object error
            'Passwords are hashed and only resets are enabled.
            Dim password As String = usr.ResetPassword(PasswordRecovery1.Answer)
 
            MsgBox(password.ToString)
 
            e.Message.From = New MailAddress("test@test.com")
            e.Message.CC.Add(New MailAddress("devemail@test.com"))
            e.Message.Subject = TryCast(GetGlobalResourceObject("Emails", "PasswordResetSubject"), String)
            'Perform subsitution
 
            strFinalBody = strFinalBody.Replace("&lt;% UserName %&gt;", PasswordRecovery1.UserName)
            strFinalBody = strFinalBody.Replace("<% Password %>", password)
 
            strFinalBody = strFinalBody.Replace("<%UserName%>", PasswordRecovery1.UserName)
            strFinalBody = strFinalBody.Replace("<%Password%>", password)
 
            strFinalBody = strFinalBody.Replace("&lt;% UserName %&gt;", PasswordRecovery1.UserName)
            strFinalBody = strFinalBody.Replace("&lt;% Password %&gt;", password)
 
            strFinalBody = strFinalBody.Replace("&lt;%UserName%&gt;", PasswordRecovery1.UserName)
            strFinalBody = strFinalBody.Replace("&lt;%Password%&gt;", password)
 
            e.Message.Body = strFinalBody
            e.Message.IsBodyHtml = True
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
            Response.Redirect("~/Errors/Error.aspx")
        End Try
    End Sub

Open in new window

Avatar of TimSledge
TimSledge
Flag of United States of America image

FireMail by ceTe Software might be helpful to you.

http://www.cete.com/Products/FireMailForNET/index.csp
Avatar of endrec
endrec

ASKER

Please note I just want to have the username and password token replacement work in the PasswordRecovery control by overriding the SendingMail method of the PasswordRecovery control.
ASKER CERTIFIED SOLUTION
Avatar of ilaird
ilaird

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial