This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes How To Find out the Password from the database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How To Find out the Password from the database" Watch "How To Find out the Password from the database" New topic
Author

How To Find out the Password from the database

Rajpal Kandhari
Ranch Hand

Joined: Aug 26, 2000
Posts: 126
Well i am working on this situation :
Have to find out the password from the databse knowing the e-mail address of the user. Same this as it is used in This forum in case any one forget the password. I can submit the query to the database and can retrive the password from the databse. Now to prevent the missuse and to provide security i have to send this password to the found e-mail. So how can i do this automatically. How can i send the e-mail to the user automatically using the input e-mail address...compared to the database e-mail address.
Can any one help me with some code, pls.
Thnnks,
Regards
Raj.


Regards,<P>Raj.<BR>-------------------------<BR>Afforts should be Appriciated.<BR>-------------------------
Sandeep Jain
Ranch Hand

Joined: Oct 25, 2000
Posts: 124
Hi,
Querying the database part is as per the normal procedure. Using the select query you can find out whether the record exist or not and then get the password from the database .Now the problem is how to send it to the user's email account .
You can do this in two ways
1) using the SmtpClient
2) using java mail package.
if u are using the SmtpClient the cose is as follows .
import sum.net.smtp.*;
Inthe servcice method you can write the following Code
SmtpClient smClient=new SmtpClient("MmailHostName");
//mailHostName can be like 'mail.webveda.com'
smClient.from("yourEmailAddress");
smClient.to("recipient email address");
PrintStream ps=smClient.startMessage();
ps.println("from "+ "yourEmailAddress");
ps.println("from "+ "recipient email address");
ps.println("write the subject that u wanna writeeg ur
password is xyz12344 or something ");
smClient.closeServer();
To find out whether the mail has been send or not put the entire code in try block .If the mail cannot be send because of some problem an exception will raise.
the second method is using the javax mail package u can send the mail .
------------------
Sandeep Jain


Try and Try Till u succeed<br /> <br />Sandeep Jain
lokesh reddy
Ranch Hand

Joined: Sep 15, 2000
Posts: 66
Hi Raj,

Here is the code that can be useful to you.
Just Imagine that my database name is email and the table name is passwd. You just prepare an html file asking the user to enter his/her email address.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import sun.net.smtp.*;
public class authenticate extends HttpServlet
{
private Connection con;
private Statement stat;
private SmtpClient client;
public void init(ServletConfig config) throws ServletException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:email");
client=new SmtpClient("MAIL_HOST")//replace
//MAIL_HOST with proper host.
}
catch(ClassNotFoundException cnfe)
{
getServletContext().log(cnfe,"Could not found class");
}
}

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String uaddr=req.getParameter("uaddress");//uaddress=user
//email address
String sql="select password from passwd";//password is
//field name and passwd is table name.
try
{
ResultSet r=stat.executeQuery(sql);
if(r!=null)
{
r.next();
String p=r.getString(1);
if(p!=null)
{
client.from("team@urname.com");
client.to(uaddr);
PrintStream ps=client.startMessage();
ps.println("From: "+"team@urname.com");
ps.println("To: "+uaddr);
ps.println("Subject: Your Password");
ps.println("Dear user here is ur password "+p);
client.closeServer();
}
}
}
catch(SQLException sqe)
{
getServletContext().log(sqe,sqe.toString());
}
catch(Exception e)
{
getServletContext().log(e,"Exception occured");
}
}
public void destroy()
{
try
{
if(con!=null)
{
con=null;
}
}
catch(SQLException sqe)
{
getServletContext().log(sqe,sqe.toString());
}
}
}
Raj i dint tested this code, but i am sure that it works fine, if anything goes wrong please intimate me.
I hope this code will be useful to you.
Loke.
[This message has been edited by lokesh reddy (edited October 27, 2000).]
Rajpal Kandhari
Ranch Hand

Joined: Aug 26, 2000
Posts: 126
Hello Sandeep & Lokesh,
Thanks for you're help and code. Lokesh i tried to compile you're code. But i am having pb with the line
client = new SmtpClient("Mail_Host");
//you have asked me to enter a mail host name.
I am working on win98 machine and have Outlook Express installed. Well this work?
I want to run the code on my system and see if this thing is working or not. I don't have a net connection on my system, I surf from the cafe. Do i need a net connection to try out the code.? I am confused as how to make it work. I made the database and entered values in them. But i am getting a Null Pointer Exception on the above mention line. Pls advice me as what to do.
Regards,
Raj.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I think the intention in the code example is that you replace "Mail_Host" with the name or IP address of the SMTP server you are trying to send mail with. Look at the properties in Outlook Express and see what the SMTP mail host is set to, then put the same in your code.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
lokesh reddy
Ranch Hand

Joined: Sep 15, 2000
Posts: 66
Hi Raj,
You please replace the "MAIL_HOST" with proper smtp server ip address or host name. If you want to try, you can check by replacing the "MAIL HOST" with the following address.
1) mail.sigmaonline.net
2) mail.webveda.com
Raj, you need to have net connection to make this program execute. But, even in a cyber cafe also you can run it. If it doesn't work there, then i think that you are behind a proxy server. If u r really behind a proxy server, then please add these two lines of code in the init method.
Properties prop=System.getProperties();
prop.put("http.proxyHost","Server Host Name") //Replace Server
//Host Name with your Server IP Address.
prop.put("http.proxyPort","Server Port") //Replace Server Port
//with the Port Number of your server.
For ur convenience i am giving an example here.
***********************************************
Properties prop=System.getProperties();
prop.put("http.proxyHost","197.100.0.1");
prop.put("http.proxyPort","80");
***********************************************
Remember to add these two line in the begining of the init method.
Loke.
Rajpal Kandhari
Ranch Hand

Joined: Aug 26, 2000
Posts: 126
Hello Frank and Lokesh,
Thanks for you're help.

Lokesh I will try to run the code at the cafe but first i have to install necessary software their. It can take some time. I will come back to you when it is done. Thanks.

Regards,
Raj.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How To Find out the Password from the database
 
Similar Threads
how to forward page as a mail
PASSED TOROLAB 141 BUT...?
Connecting 2 GUI
e-mail sender!
mail using jsp