Saturday, March 12, 2011

Asp.net Email to Recover or Reset Password


RecoverPassword.aspx
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="RecoverPassword.aspx.cs" Inherits="RecoverPassword" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title>Untitled Page</title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
    <h2>Recover Your Password</h2>  
   <p>  
     <asp:PasswordRecovery ID="RecoverPwd" runat="server"  
       onsendingmail="RecoverPwd_SendingMail">  
       <MailDefinition BodyFileName="~/PasswordRecovery.txt"  
         Subject="Your password has been reset...">  
       </MailDefinition>  
     </asp:PasswordRecovery>  
   </p>  
   </form>  
 </body>  
 </html>  

RecoverPassword.aspx.cs
 using System.Net.Mail;  
 using System.Net;  
 public partial class RecoverPassword : System.Web.UI.Page  
 {  
   protected void Page_Load(object sender, EventArgs e)  
   {  
     MailMessage myMessage = new MailMessage();  
     SmtpClient mySmtpClient = new SmtpClient();  
     myMessage.To.Add(new MailAddress("youraddress@example.com"));  
     mySmtpClient.Port = 587;  
     mySmtpClient.Credentials = new System.Net.NetworkCredential("admin emailid","password");  
     mySmtpClient.EnableSsl = true;  
   }  
   protected void RecoverPwd_SendingMail(object sender, MailMessageEventArgs e)  
   {  
     SmtpClient mySmtpClient = new SmtpClient();  
     mySmtpClient.EnableSsl = true;  
     mySmtpClient.Send(e.Message);  
     e.Cancel = true;  
   }  
 }  

PasswordRecovery.txt
you can also use text file to send email rather than using html.using html make email message good in look but in text thats not avalable.
 Your password has been reset, <%UserName%>!  
 According to our records, you have requested that your password be reset. Your new password is: <%Password%>  
 If you have any questions or trouble logging on please contact a site administrator.  
 Thank you!  


IIS setting for smtp remain same as in Email to verify your a/c

No comments:

Popular Posts