0

我必须从 MySQL 数据库中获取密码。该密码使用 JDBC 发送到 Java 中的用户有效电子邮件。我该如何实施?

这是我正在使用的代码:

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;
    }
}

在这里,我已经成功检查了电子邮件是有效还是无效。

如何编写获取密码的代码并将这些密码发送到电子邮件功能?

编辑:

现在我更改了如下代码:

    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;

                              }


                                } 

在这里,我在我的代码中公开提到了我的 gmail 用户名和密码。

       Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                  protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("xxxxx@gmail.com","xxxx");
                        }
                    });

我已经发送了没有提及 gmail 用户名和密码的邮件。请帮助我。我该如何开发这些。

4

1 回答 1

0

您应该使用有效的主机

这可能会帮助你..

      String host = "smtp.gmail.com"; // Host for Gmail
      Properties props = System.getProperties();
      props.setProperty("mail.user", from);
      props.setProperty("mail.password", retrievedPassword);
      props.put("mail.smtp.host", host); 
于 2012-12-14T08:50:51.670 回答