?
Solved

Send user's password to their email, using the email entered.

Posted on 2003-03-01
18
Medium Priority
?
377 Views
Last Modified: 2010-04-01
Hi all

I am trying to make a script/form that sends user's password to their email, using the email entered. Of course the email must exist in the DB.
Can anyone help me on this. Doesn't necessary have to be in jsp but in cgi or php as well.

Thanx
0
Comment
Question by:hight3ch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 5
18 Comments
 
LVL 14

Expert Comment

by:kennethxu
ID: 8047684
read password from database:
<%@page import="java.sql.*" %>
<%
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String email = request.getParameter( "email" );
String password = null;

try {
     Class.forName ("com.mysql.jdbc.Driver"); //this is MySQL example, replace it with your jdbc driver
    conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "dbuser", "dbpassword");
    ps = conn.PrepareStatement( "select password from user_table where email = ?" );
     ps.setString( 1, email );
    rs = ps.executeQuery();
    if(rs.next()) password = rs.getString(1);
} finally {
    if( rs != null ) try { rs.close(); } catch( Exception e ) {}
    if( ps != null ) try { ps.close(); } catch( Exception e ) {}
    if( conn != null ) try { conn.close(); } catch( Exception e ) {}
}

// if we find the email in db and get the password
if( password ! = null ) {

     // Get system properties
     Properties props = System.getProperties();

     // Setup mail server
     props.put("mail.smtp.host", "my.smtp.server.address" );

     // Get session
     Session session = Session.getDefaultInstance(props, null);

     // Define message
     MimeMessage message = new MimeMessage(session);
     message.setFrom(new InternetAddress( "me@mydomain.com" ));
     message.addRecipient(Message.RecipientType.TO, new InternetAddress( email ));
     message.setSubject("Your Request for Password");
     message.setText("Your password is: " + password );

     // Send message
     Transport.send(message);

} else {
     out.println( "your email address is not found in our database" );
}

%>

0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8047686
more info on how to use javamail can be find here:
http://developer.java.sun.com/developer/onlineTraining/JavaMail/contents.html

let me know if you have further enquires.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8047691
sorry for typo, it should be:
ps = conn.prepareStatement( "select password from user_table where email = ?" );
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:hight3ch
ID: 8049068
kennethxu thank you for you help I managed to set up the javamail api etc and manage to compile and run the test program 'msgsend' (from demos msgsend.java) which means I installed the api correctly. I have a problem though...
When I run the jsp page I get the following error:

---------------------------------------


91.     MimeMessage message = new MimeMessage(session);
            <--------->
*** Error: Type jsp/MimeMessage was not found


    91.     MimeMessage message = new MimeMessage(session);
                                  <---------------------->
*** Error: No match was found for constructor "MimeMessage(javax.servlet.http.HttpSession)"


    92.     message.setFrom(new InternetAddress( "me@mydomain.com" ));
                            <-------------------------------------->
*** Error: No match was found for constructor "InternetAddress(java.lang.String)"


    92.     message.setFrom(new InternetAddress( "me@mydomain.com" ));
                                <------------->
*** Error: Type jsp/InternetAddress was not found


    93.     message.addRecipient(Message.RecipientType.TO, new InternetAddress( email ));
                                 <---------------------->
*** Error: "Message/RecipientType/TO" is either a misplaced package name or a non-existent entity. An expression name is expected in this context


    93.     message.addRecipient(Message.RecipientType.TO, new InternetAddress( email ));
                                                           <-------------------------->
*** Error: No match was found for constructor "InternetAddress(java.lang.String)"


    98.     Transport.send(message);
            <------->
*** Error: "Transport" is either a misplaced package name or a non-existent entity.


-------------------------------------------------

which means I must import the :

<%@page import="javax.mail.*" %>
<%@page import="javax.mail.internet.*" %>

correct?

but when I do it I get the following:

------------------------------------------------

29. import javax.mail.*;
               <-------->
*** Error: Could not find package named:
                C:\JRun\jsm-default\classes/javax/mail,
                C:\JRun\lib\jrun.jar(javax/mail),
                C:\JRun\lib\servlet.jar(javax/mail),
                C:\JRun\webl\WebL.jar(javax/mail),
                C:\JRun\lib\jsp.jar(javax/mail),
                C:\JRun\lib\xml4j.jar(javax/mail),
                C:\JRun\lib\xt.jar(javax/mail),
                C:\JRun\lib\fesi.jar(javax/mail),
                C:\JRun\lib\cfanywhere.jar(javax/mail),
                C:\JRun\lib\NetComponents.jar(javax/mail),
                C:\JRun\lib\OROMatcher.jar(javax/mail),
                C:\JRun\lib\tools.jar(javax/mail),
                C:\JRun\instantdb\Classes/javax/mail,
                C:\JRun\lib\jrunadmin\jrunadmin.jar(javax/mail),
                C:\jdk1.3.1\jre\lib\rt.jar(javax/mail),
                C:\jdk1.3.1\jre\lib\i18n.jar(javax/mail),
                C:\jdk1.3.1\lib\tools.jar(javax/mail) or
                C:\JRun\lib\jrunadmin\swing.jar(javax/mail)


    31. import javax.mail.internet.*;
               <----------------->
*** Error: Could not find package named:
                C:\JRun\jsm-default\classes/javax/mail/internet,
                C:\JRun\lib\jrun.jar(javax/mail/internet),
                C:\JRun\lib\servlet.jar(javax/mail/internet),
                C:\JRun\webl\WebL.jar(javax/mail/internet),
                C:\JRun\lib\jsp.jar(javax/mail/internet),
                C:\JRun\lib\xml4j.jar(javax/mail/internet),
                C:\JRun\lib\xt.jar(javax/mail/internet),
                C:\JRun\lib\fesi.jar(javax/mail/internet),
                C:\JRun\lib\cfanywhere.jar(javax/mail/internet),
                C:\JRun\lib\NetComponents.jar(javax/mail/internet),
                C:\JRun\lib\OROMatcher.jar(javax/mail/internet),
                C:\JRun\lib\tools.jar(javax/mail/internet),
                C:\JRun\instantdb\Classes/javax/mail/internet,
                C:\JRun\lib\jrunadmin\jrunadmin.jar(javax/mail/internet),
                C:\jdk1.3.1\jre\lib\rt.jar(javax/mail/internet),
                C:\jdk1.3.1\jre\lib\i18n.jar(javax/mail/internet),
                C:\jdk1.3.1\lib\tools.jar(javax/mail/internet) or
                C:\JRun\lib\jrunadmin\swing.jar(javax/mail/internet)

---------------------------------------------

can you give me a help, what should i do, I would much appreciate this.

Thanx in advance
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8049337
<%@page import="java.sql.*, javax.mail.*" %>

change all "Session" to "javax.mail.Session", and change "session" to "mailSession"

let me know if you have further enquires, thanks.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8049345
>> change all "Session" to "javax.mail.Session", and change "session" to "mailSession"
sorry, you don't have to change "Session", but must change "session" var to "mailSession".
0
 

Author Comment

by:hight3ch
ID: 8049662
I managed to fix it and seems to be working although it doesn't send an email. Apparently it throws the following exception:

No provider for Address type: rfc822

Can you figure out what's happening?

I played a bit with the code here it is:

-------------------------------------------------

<%@page import="java.sql.*, javax.mail.*, javax.mail.internet.*" %>

<%
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String email = request.getParameter( "email" );
String password = null;

try
{
        Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); //this is MySQL example, replace it with your jdbc driver
     conn = DriverManager.getConnection("jdbc:odbc:KPC");
   
     ps = conn.prepareStatement("SELECT password FROM Users WHERE email = ?");
        ps.setString( 1, email );
     rs = ps.executeQuery();
     
     //System.out.println(rs);
     //System.out.println(ps);
     //System.out.println(conn);
     
        if(rs.next())
     {
          password = rs.getString(1);
          //System.out.println("["+password+"]");
     }

     // if we find the email in db and get the password
     if( password != null )
     {
          // Get system properties
         Properties props = System.getProperties();
         
          // Setup mail server
         props.put("mail.smtp.host", "mail.btopenworld.com" );
         
         // Get session
         Session mailSession = Session.getDefaultInstance(props, null);
         
         // Define message
         MimeMessage message = new MimeMessage(mailSession);
         message.setFrom(new InternetAddress("aris@websmart.gr"));
             message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));
          message.setSubject("Your Request for Password");
         message.setText("Your password is: " + password );
         
          // Send message
         Transport.send(message);

          out.println("\nMail was sent successfully!");
     }
     
     else
     {
         out.println( "Your email address was not found in our database" );
     }
     
}
catch( Exception e )
{
     System.out.println("Exception: " + e.getMessage());
}

%>


-------------------------------------------------
0
 

Author Comment

by:hight3ch
ID: 8049668
continue from the previous....

the final step:

Transport.send(message);

does not execute for some reason.

Thanx
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 400 total points
ID: 8049787
can you check if you have entered correct email address. the error message means an incorrect email address is entered.

this code works for me, i can send email to my own yahoo address.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8049788
can you check if you have entered correct email address. the error message means an incorrect email address is entered.

this code works for me, i can send email to my own yahoo address.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8049789
<%@page import="java.util.*, java.sql.*, javax.mail.*, javax.mail.internet.*" %>

<%
String email = "xxxxxxxxxx@yahoo.com";

try {
    // if we find the email in db and get the password
         // Get system properties
        Properties props = System.getProperties();
         
         // Setup mail server
        props.put("mail.smtp.host", "mta108.mail.scd.yahoo.com" );
         
        // Get session
        Session mailSession = Session.getDefaultInstance(props, null);
         
        // Define message
        MimeMessage message = new MimeMessage(mailSession);
        message.setFrom(new InternetAddress("aris@websmart.gr"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));
         message.setSubject("Test Message");
        message.setText("Message body" );
         
         // Send message
        Transport.send(message);

         out.println("\nMail was sent successfully!");
} catch( Exception e ) {
     e.printStackTrace();
}  
%>
0
 

Author Comment

by:hight3ch
ID: 8050160
Problem solved, everything works fine!
Had to configure properly the server!

Thank you for everything.

:-)

0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8050255
my pleasure to help and glad to know your problem is solved :)
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8060522
hi hight3ch, can we PAQ this question or you need any futher help?
0
 

Author Comment

by:hight3ch
ID: 8060651
Yes we can again many thanx
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8060735
to PAQ this question, you need to accept comment as answer. the link is right above every comment. you'll be prompted to gave a grade to the answer, then the points and grades will credit into my account.

of course, if my comments didn't help, you can ask EE support to refund your points :)

Thanks.
0
 
LVL 35

Expert Comment

by:girionis
ID: 10040890
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to kennethxu

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question