I have to fetch the password from MySQL database. That password is send to user valid email in Java using JDBC. How can I implement this?
Here is the code I am using:
public class Checkemail
{
public String authentication( String Email ) {
String retrievedUserName = "";
String retrievedPassword = "";
String status = "";
try {
Class.forName( "com.mysql.jdbc.Driver" );
Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/xcart-432pro", "root", "" );
PreparedStatement statement = con.prepareStatement( "SELECT * FROM xcart_customers WHERE email = '" + Email + "'" );
ResultSet result = statement.executeQuery();
while( result.next() ) {
retrievedUserName = result.getString( "email" );
retrievedPassword = result.getString("password");
}
if( retrievedUserName.equals( Email ) ) {
status = "Valid Email";
}
else {
status = "Invalid Email!!!";
}
}
catch( Exception e ) {
e.printStackTrace();
}
return status;
}
}
Here I have checked the email is valid or invalid successfully.
How can I write the code for fetching the password and these password is send to email function?
EDIT:
Now i have changed the code like below:
if(retrievedUserName.equals(Email)){
status = "Valid Email";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxxxx@gmail.com","xxxx");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("krishnaveni.veeman@mercuryminds.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(Email));
message.setSubject("Testing Subject");
message.setText("Dear Friends This is your fassword," +
retrievedPassword);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
else{
status = "Invalid Email!!!";
}
}
catch(Exception e){
e.printStackTrace();
}
return status;
}
}
Here i have mentioned my gmail username and password on my code openly.
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxxxx@gmail.com","xxxx");
}
});
I have send mail w/o mention gmail username and password.please help me.how can i develop these.
You should use valid host.
This may help you..