Bonjour à tous,
Je termine l'application que j'ai du réaliser pendant mon stage, et je dois régler les derniers détails. Devant faire un intranet, ayant coder les mots de passe en md5, je souhaiterais pouvoir faire une fonctionnalité permettant de récupérer un nouveau mot de passe en cas de perte. Je demande donc l'identifiant et l'adresse mail de l'utilisateur, et je souhaiterais lui envoyé un mail automatiquement avec ce nouveau mot de passe. Voila la classe que j'ai telecharger :
Et voila le code de ma servlet qui me servirait a envoyé ce mail :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 import java.io.IOException; import java.io.PrintWriter; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestMailService extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter writer = response.getWriter(); response.setContentType("text/html"); writer.println("<html><head><title>Mail Example</title></head>"); writer.println("<body bgcolor=\"white\">"); writer.println("<h1>Mail Example</h1>"); ///////// set this variable to be your SMTP host String mailHost = "your.smtp.server"; ///////// set this variable to be your desired email recipient String to = "recipient@recip.recip"; // these variables come from the mail form String from = request.getParameter("from"); String subject = request.getParameter("subject"); String body = request.getParameter("body"); if ((from != null) && (to != null) && (subject != null) && (body != null)) // we have mail to send { try { //Get system properties Properties props = System.getProperties(); //Specify the desired SMTP server props.put("mail.smtp.host", mailHost); // create a new Session object Session session = Session.getInstance(props,null); // create a new MimeMessage object (using the Session created above) Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(to) }); message.setSubject(subject); message.setContent(body, "text/plain"); Transport.send(message); // it worked! writer.println("<b>Thank you. Your message to " + to + " was successfully sent.</b>"); } catch (Throwable t) { writer.println("<b>Unable to send message: <br><pre>"); t.printStackTrace(writer); writer.println("</pre></b>"); } } else { // no mail to send. print a blank mail form writer.println("<form action=\"/mine/mail\" method=\"POST\">"); writer.println("<table border=\"0\">"); writer.println("<tr><td>Message From: </td><td><input type=\"text\" name=\"from\"></td></tr>"); writer.println("<tr><td>Subject: </td><td><input type=\"text\" name=\"subject\"></td></tr>"); writer.println("<tr><td valign=\"top\">Message: </td><td><textarea name=\"body\" rows=\"10\" cols\"40\"></textarea></td></tr>"); writer.println("<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"Send\"></td></tr>"); writer.println("</table>"); writer.println("</form>"); } writer.println("</body>"); writer.println("</html>"); } // doGet() just calls doPost() public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doPost(request, response); } }
Je ne parviens cependant pas a comprendre comment utiliser ce service testMailService dans ma servlet
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.bi.projecttracking.dao.UserDO; import com.bi.projecttracking.services.AuthenticationService; import com.bi.projecttracking.services.ConfigurationService; import com.bi.projecttracking.services.LoggingService; import com.bi.projecttracking.services.Md5Service; import com.bi.projecttracking.services.TestMailService; import com.bi.projecttracking.tools.Tools; /** * Servlet implementation class for Servlet: NewPasswordServlet * */ public class NewPasswordServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#HttpServlet() */ public NewPasswordServlet() { super(); } /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //instanciate all the required Services AuthenticationService authenticationService = new AuthenticationService(); ConfigurationService configService = new ConfigurationService(); LoggingService loggingService = new LoggingService(configService.getProperties()); Md5Service md5Service = new Md5Service(); TestMailService testMailService = new TestMailService(); String login = request.getParameter("login"); String mail = request.getParameter("mail"); String jspForward = ""; //put all the attributes in an ArrayList HashMap mandatoryFields = new HashMap(); mandatoryFields.put("login", login); mandatoryFields.put("mail", mail); //check that the form is correctly filled ArrayList uncompleteFields = Tools.checkAttributes(mandatoryFields); loggingService.log("uncompleteFields : " + uncompleteFields.size(), LoggingService.DEBUG); //check that the login exist on the database UserDO user = authenticationService.getUser(login); Boolean userExist = user != null? true:false; loggingService.log("userExist : " + userExist, LoggingService.DEBUG); if (uncompleteFields != null && uncompleteFields.size() == 0 && userExist) { // we generate a new password String newPass = "bi-"+login; //we encrypted this new password String newPassEncrypted = md5Service.encode(newPass); // we edit the password of this user in the database user.setPassword(newPassEncrypted); //authenticationService.editUser(user); jspForward = "/jsp/Login.jsp"; //set the inforrmations request.setAttribute("sendPass",true); request.setAttribute("mail",mail); } else { jspForward = "/jsp/ForgetPassword.jsp"; request.setAttribute("uncompleteFields",uncompleteFields); request.setAttribute("userExist", userExist); if (login!=null) { request.setAttribute("login",login); } if (mail!=null) { request.setAttribute("mail",mail); } } response.setContentType("text/html"); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(jspForward); dispatcher.forward(request,response); } }.
Si quelqu'un a une idée, je le remerci d'avance !!
A+
Partager