Send Random Password in Mail in ASP.Net

You have seen in many registration form you have only username and email field and the password will be send to your respective mail id, So the same we will do in the application.

Step 1

Open your Visual Studio 2010 and create an Empty Website, provide a suitable name (randompass_demo).

Step 2

In Solution Explorer you get your empty website, then add two web forms and a SQL Server Database as in the following.

For Web Form:

randompass_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it randompass_demo.aspx.

For SQL Server Database

randompass_demo (your empty website) then right-click then select Add New Item -> SQL Server Database. (Add a database inside the App_Data_folder).

DATABASE CHAMBER

Step 3

In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table. Make the table like this.

Table tbl_data (Don't Forget to make ID as IS Identity -- True)

table

Design Chamber

Step 4

Open your randompass_demo.aspx file from Solution Explorer and start designing you're application.

Randompass_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <style type="text/css">  
  9.         .style1  
  10.         {  
  11.             width: 196px;  
  12.         }  
  13.         .style2  
  14.         {  
  15.             font-size: large;  
  16.             text-decoration: underline;  
  17.             text-align: left;  
  18.             color: #FF3300;  
  19.         }  
  20.     </style>  
  21. </head>  
  22. <body>  
  23.     <form id="form1" runat="server">  
  24.     <div>  
  25.       
  26.         <table style="width:100%;">  
  27.             <caption class="style2">  
  28.                 <strong>Random Password In Email</strong></caption>  
  29.             <tr>  
  30.                 <td class="style1">  
  31.                     Username:</td>  
  32.                 <td>  
  33.                     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  34.                 </td>  
  35.                 <td>  
  36.                      </td>  
  37.             </tr>  
  38.             <tr>  
  39.                 <td class="style1">  
  40.                     Email:</td>  
  41.                 <td>  
  42.         <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>  
  43.                 </td>  
  44.                 <td>  
  45.                      </td>  
  46.             </tr>  
  47.             <tr>  
  48.                 <td class="style1">  
  49.                      </td>  
  50.                 <td>  
  51.                      </td>  
  52.                 <td>  
  53.                      </td>  
  54.             </tr>  
  55.             <tr>  
  56.                 <td class="style1">  
  57.         <asp:Label ID="lblMessage" runat="server"></asp:Label>  
  58.       
  59.                 </td>  
  60.                 <td>  
  61.     <asp:Button ID="Button1" runat="server" onclick="Button1_Click1"   
  62.         Text="Submit" />  
  63.                 </td>  
  64.                 <td>  
  65.                      </td>  
  66.             </tr>  
  67.         </table>  
  68.       
  69.     </div>  
  70.     </form>  
  71. </body>  
  72. </html>  
Your design will look like this:

design

Code Chamber

Step 5

Finally open your randompass_demo.aspx.cs file to write the code to make the application as we designed it.

Randompass_demo.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Net;  
  8. using System.Net.Mail;  
  9. using System.Data;  
  10. using System.Data.SqlClient;  
  11.   
  12.   
  13. public partial class _Default: System.Web.UI.Page {  
  14.     protected void Page_Load(object sender, EventArgs e) {  
  15.         GeneratePassword();  
  16.     }  
  17.   
  18.     public string GeneratePassword() {  
  19.         string PasswordLength = "8";  
  20.         string NewPassword = "";  
  21.   
  22.         string allowedChars = "";  
  23.         allowedChars = "1,2,3,4,5,6,7,8,9,0";  
  24.         allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";  
  25.         allowedChars += "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";  
  26.   
  27.   
  28.         char[] sep = {  
  29.             ','  
  30.         };  
  31.         string[] arr = allowedChars.Split(sep);  
  32.   
  33.   
  34.         string IDString = "";  
  35.         string temp = "";  
  36.   
  37.         Random rand = new Random();  
  38.   
  39.         for (int i = 0; i < Convert.ToInt32(PasswordLength); i++) {  
  40.             temp = arr[rand.Next(0, arr.Length)];  
  41.             IDString += temp;  
  42.             NewPassword = IDString;  
  43.   
  44.         }  
  45.         return NewPassword;  
  46.     }  
  47.   
  48.     protected void Button1_Click1(object sender, EventArgs e) {  
  49.         // to save the username and password in database  
  50.         SqlConnection con = new SqlConnection(@  
  51.         "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  52.         SqlCommand cmd = new SqlCommand("insert into tbl_data(username,email) values (@username,@email)", con);  
  53.         cmd.Parameters.AddWithValue("username", TextBox1.Text);  
  54.         cmd.Parameters.AddWithValue("email", txtEmail.Text);  
  55.         con.Open();  
  56.         int i = cmd.ExecuteNonQuery();  
  57.         con.Close();  
  58.   
  59.   
  60.         // to send the random password in email  
  61.   
  62.         string strNewPassword = GeneratePassword().ToString();  
  63.   
  64.         MailMessage msg = new MailMessage();  
  65.         msg.From = new MailAddress("[email protected]");  
  66.         msg.To.Add(txtEmail.Text);  
  67.         msg.Subject = "Random Password for your Account";  
  68.         msg.Body = "Your Random password is:" + strNewPassword;  
  69.         msg.IsBodyHtml = true;  
  70.   
  71.         SmtpClient smt = new SmtpClient();  
  72.         smt.Host = "smtp.gmail.com";  
  73.         System.Net.NetworkCredential ntwd = new NetworkCredential();  
  74.         ntwd.UserName = "[email protected]"//Your Email ID  
  75.         ntwd.Password = ""// Your Password  
  76.         smt.UseDefaultCredentials = true;  
  77.         smt.Credentials = ntwd;  
  78.         smt.Port = 587;  
  79.         smt.EnableSsl = true;  
  80.         smt.Send(msg);  
  81.         lblMessage.Text = "Email Sent Successfully";  
  82.         lblMessage.ForeColor = System.Drawing.Color.ForestGreen;  
  83.   
  84.     }  
  85. }  
So here we made a GeneratePassword() method and called it in the section to send mail, so when you click on the button, the code will eventually save your username and email and it also sends the password to your respective mail id.

Output Chamber

Output

Here I gave the input as username = Nilesh and email as = [email protected], it will save that to the database and the password will be generated and sent to the mail id.

password

Your Random Password

Random Password

Data Saved to Database:

data

I hope you like this. Thank you for reading, have a good day.


Similar Articles