Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.ResultSet;
- import eskuel.CMI_SQL;
- import java.io.IOException;
- import java.sql.SQLException;
- import java.util.Properties;
- import javax.mail.AuthenticationFailedException;
- import javax.mail.Authenticator;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.AddressException;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class beheer_mailing_stuurmail extends HttpServlet
- {
- protected void processRequest(HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException
- {
- final String err = "/beheer_home.jsp";
- final String succ = "/beheer_mailing.jsp";
- String rnummer = "";
- String email = "";
- String to = "";
- CMI_SQL sql = new CMI_SQL("jdbc:mysql://mysql.cmi-hro.nl:3306/cmi0815283", "cmi0815283", "bla");
- 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) ";
- ResultSet resultaat = sql.selectieQuery(query);
- try
- {
- while (resultaat.next())
- {
- rnummer = resultaat.getString("reserveringsnummer");
- String query2 = "SELECT emailadres FROM KLANT_GEGEVENS where reserveringsnummer = '" + rnummer + "'";
- ResultSet resultaat2 = sql.selectieQuery(query2);
- while (resultaat2.next())
- {
- email = resultaat2.getString("emailadres");
- to += '<' + email + ">,";
- }
- }
- }
- catch (SQLException e_sql)
- {
- }
- // String gegevens = (String) request.getSession().getAttribute("gegevens");
- String from = "jfb.inf1a@gmail.com";
- String subject = "Review";
- String message = "Blablablablalbalblabalalblablablabab";
- String login = "jfb.inf1a@gmail.com";
- String password = "adminjfb";
- try
- {
- Properties props = new Properties();
- props.setProperty("mail.host", "smtp.gmail.com");
- props.setProperty("mail.smtp.port", "587");
- props.setProperty("mail.smtp.auth", "true");
- props.setProperty("mail.smtp.starttls.enable", "true");
- Authenticator auth = new SMTPAuthenticator(login, password);
- Session session = Session.getInstance(props, auth);
- MimeMessage msg = new MimeMessage(session);
- msg.setText(message);
- msg.setSubject(subject);
- msg.setFrom(new InternetAddress(from));
- msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
- Transport.send(msg);
- }
- catch (AuthenticationFailedException ex)
- {
- request.setAttribute("ErrorMessage", "Authentication failed");
- RequestDispatcher dispatcher = request.getRequestDispatcher(err);
- dispatcher.forward(request, response);
- }
- catch (AddressException ex)
- {
- request.setAttribute("ErrorMessage", "Wrong email address");
- RequestDispatcher dispatcher = request.getRequestDispatcher(err);
- dispatcher.forward(request, response);
- }
- catch (MessagingException ex)
- {
- request.setAttribute("ErrorMessage", ex.getMessage());
- RequestDispatcher dispatcher = request.getRequestDispatcher(err);
- dispatcher.forward(request, response);
- }
- RequestDispatcher dispatcher = request.getRequestDispatcher(succ);
- dispatcher.forward(request, response);
- }
- private class SMTPAuthenticator extends Authenticator
- {
- private PasswordAuthentication authentication;
- public SMTPAuthenticator(String login, String password)
- {
- authentication = new PasswordAuthentication(login, password);
- }
- protected PasswordAuthentication getPasswordAuthentication()
- {
- return authentication;
- }
- }
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException
- {
- processRequest(request, response);
- }
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException
- {
- processRequest(request, response);
- }
- }
Add Comment
Please, Sign In to add comment