Guest User

Untitled

a guest
Jun 1st, 2018
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1.  
  2. import java.sql.ResultSet;
  3. import eskuel.CMI_SQL;
  4. import java.io.IOException;
  5. import java.sql.SQLException;
  6.  
  7. import java.util.Properties;
  8. import javax.mail.AuthenticationFailedException;
  9. import javax.mail.Authenticator;
  10. import javax.mail.PasswordAuthentication;
  11. import javax.mail.Message;
  12. import javax.mail.MessagingException;
  13. import javax.mail.Session;
  14. import javax.mail.Transport;
  15. import javax.mail.internet.AddressException;
  16. import javax.mail.internet.InternetAddress;
  17. import javax.mail.internet.MimeMessage;
  18. import javax.servlet.*;
  19. import javax.servlet.http.*;
  20.  
  21. public class beheer_mailing_stuurmail extends HttpServlet
  22. {
  23.  
  24. protected void processRequest(HttpServletRequest request,
  25. HttpServletResponse response)
  26. throws IOException, ServletException
  27. {
  28.  
  29. final String err = "/beheer_home.jsp";
  30. final String succ = "/beheer_mailing.jsp";
  31.  
  32.  
  33. String rnummer = "";
  34.  
  35. String email = "";
  36. String to = "";
  37.  
  38. CMI_SQL sql = new CMI_SQL("jdbc:mysql://mysql.cmi-hro.nl:3306/cmi0815283", "cmi0815283", "bla");
  39. String query = "SELECT KLANT.reserveringsnummer FROM KLANT where KLANT.reserveringsnummer NOT IN (SELECT KLANT.reserveringsnummer FROM KLANT, REVIEW_1 where REVIEW_1.reserveringsnummer = KLANT.reserveringsnummer) ";
  40. ResultSet resultaat = sql.selectieQuery(query);
  41.  
  42. try
  43. {
  44. while (resultaat.next())
  45. {
  46. rnummer = resultaat.getString("reserveringsnummer");
  47.  
  48.  
  49.  
  50. String query2 = "SELECT emailadres FROM KLANT_GEGEVENS where reserveringsnummer = '" + rnummer + "'";
  51. ResultSet resultaat2 = sql.selectieQuery(query2);
  52. while (resultaat2.next())
  53. {
  54. email = resultaat2.getString("emailadres");
  55. to += '<' + email + ">,";
  56. }
  57. }
  58.  
  59. }
  60. catch (SQLException e_sql)
  61. {
  62. }
  63.  
  64. // String gegevens = (String) request.getSession().getAttribute("gegevens");
  65.  
  66.  
  67. String from = "jfb.inf1a@gmail.com";
  68.  
  69. String subject = "Review";
  70. String message = "Blablablablalbalblabalalblablablabab";
  71. String login = "jfb.inf1a@gmail.com";
  72. String password = "adminjfb";
  73.  
  74. try
  75. {
  76. Properties props = new Properties();
  77. props.setProperty("mail.host", "smtp.gmail.com");
  78. props.setProperty("mail.smtp.port", "587");
  79. props.setProperty("mail.smtp.auth", "true");
  80. props.setProperty("mail.smtp.starttls.enable", "true");
  81.  
  82. Authenticator auth = new SMTPAuthenticator(login, password);
  83.  
  84. Session session = Session.getInstance(props, auth);
  85.  
  86. MimeMessage msg = new MimeMessage(session);
  87. msg.setText(message);
  88. msg.setSubject(subject);
  89. msg.setFrom(new InternetAddress(from));
  90. msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  91. Transport.send(msg);
  92.  
  93. }
  94. catch (AuthenticationFailedException ex)
  95. {
  96. request.setAttribute("ErrorMessage", "Authentication failed");
  97.  
  98. RequestDispatcher dispatcher = request.getRequestDispatcher(err);
  99. dispatcher.forward(request, response);
  100.  
  101. }
  102. catch (AddressException ex)
  103. {
  104. request.setAttribute("ErrorMessage", "Wrong email address");
  105.  
  106. RequestDispatcher dispatcher = request.getRequestDispatcher(err);
  107. dispatcher.forward(request, response);
  108.  
  109. }
  110. catch (MessagingException ex)
  111. {
  112. request.setAttribute("ErrorMessage", ex.getMessage());
  113.  
  114. RequestDispatcher dispatcher = request.getRequestDispatcher(err);
  115. dispatcher.forward(request, response);
  116. }
  117. RequestDispatcher dispatcher = request.getRequestDispatcher(succ);
  118. dispatcher.forward(request, response);
  119.  
  120. }
  121.  
  122. private class SMTPAuthenticator extends Authenticator
  123. {
  124.  
  125. private PasswordAuthentication authentication;
  126.  
  127. public SMTPAuthenticator(String login, String password)
  128. {
  129. authentication = new PasswordAuthentication(login, password);
  130. }
  131.  
  132. protected PasswordAuthentication getPasswordAuthentication()
  133. {
  134. return authentication;
  135. }
  136. }
  137.  
  138. protected void doGet(HttpServletRequest request,
  139. HttpServletResponse response)
  140. throws ServletException, IOException
  141. {
  142. processRequest(request, response);
  143. }
  144.  
  145. protected void doPost(HttpServletRequest request,
  146. HttpServletResponse response)
  147. throws ServletException, IOException
  148. {
  149. processRequest(request, response);
  150. }
  151. }
Add Comment
Please, Sign In to add comment