Saturday, March 12, 2011

Asp.net Email to Receive Your Changed Password


ChangePassword.aspx
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChangePassword.aspx.cs" Inherits="ChangePassword" %>  
 <!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>Change Your Password</h2>  
   <p>  
     <asp:ChangePassword ID="ChangePwd" runat="server" onsendingmail="Changepwd_SendingMail">  
       <MailDefinition BodyFileName="~/ChangePassword.htm"  
         IsBodyHtml="True" Subject="Your password has been changed!">  
       </MailDefinition>  
     </asp:ChangePassword>  
   </p>  
   </form>  
 </body>  
 </html>  

ChangePassword.aspx .cs
 using System.Net.Mail;  
 public partial class ChangePassword : 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("emailid", "Password";  
     mySmtpClient.EnableSsl = true;  
   }  
   protected void Changepwd_SendingMail(object sender, MailMessageEventArgs e)  
   {  
     SmtpClient mySmtpClient = new SmtpClient();  
     mySmtpClient.EnableSsl = true;  
     mySmtpClient.Send(e.Message);  
     e.Cancel = true;  
   }  
 }  

ChangePassword.html
 <!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>  
   <title>Untitled Page</title>  
 </head>  
 <body>  
 <h2>Your Password Has Been Changed!</h2>  
   <p>  
     This email confirms that your password has been changed.  
   </p>  
   <p>  
     To log on to the site, use the following credentials:  
   </p>  
   <table>  
     <tr>  
       <td>  
         <b>Username:</b>  
       </td>  
       <td>  
         <%UserName%>  
       </td>  
     </tr>  
     <tr>  
       <td>  
         <b>Password:</b>  
       </td>  
       <td>  
         <%Password%>  
       </td>  
     </tr>  
   </table>  
   <p>  
     If you have any questions or encounter any problems logging in,  
     please contact a site administrator.  
   </p>  
 </body>  
 </html>  

Output:

IIS setting for smtp remain same as in Email for Account Verification

No comments:

Popular Posts