@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = null;
String user = request.getParameter("username");
Users p = usb.getMemberInfo(user);
HttpSession session1 = request.getSession(true);
request.getSession().setAttribute("passenger", p);
String host = "smtp.gmail.com";
String username = // to write sender email here
String password = // To Write Password here
Properties properties = System.getProperties();
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.username", username);
properties.put("mail.smtp.password", password);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
String to = (String)session1.getAttribute(p.getEmail());
try {
Session session = Session.getDefaultInstance(properties, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
InternetAddress toAddress = new InternetAddress(to);
System.out.println(Message.RecipientType.TO);
//for (int i = 0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress);
//}
message.setSubject("Forgot password");
message.setText(p.getPassword());
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
response.sendRedirect("welcome.jsp");
}
}
メソッド getMemberInfo:
@Override
public Users getMemberInfo(String username) {
String query = "SELECT M from Users M where M.username=:username";
Query q = em.createQuery(query);
q.setParameter("username", username);
List results = q.getResultList();
for (Object o:results) {
Users m = (Users) o;
return m;
}
return null;
}
問題は、メールが届かないことです。何が問題なのかわかりません。ユーザーはユーザー名を書き、データベースから電子メールを取得し、データベースから取得したユーザーの電子メールにパスワードを送信する必要があります。