public String sendPoll(Integer pollId) throws SQLException, IOException, NoSuchAlgorithmException { Poll poll = get(pollId); String[] contacts = poll.getContacts(); if (sendMailTo(contacts, poll)) { dao.sendPoll(pollId); return "The poll was send to all the contacts!"; } throw new RuntimeException("A problem ocurred when sending the mail to the contacts"); }
private Email createEmail(String contact, Poll poll, Map<String, String> replacement) throws IOException { HashSet<String> recepient = new HashSet<String>(); recepient.add(contact); Email email = new Email(); email.setFrom(Parameters.getSentFromEmail()); email.setSubject( "[Feedback Request] " + (poll.getEmailSubject() == null ? poll.getTitle() : poll.getEmailSubject())); email.setRecipients(recepient); email.setBody(createEmailBody(replacement)); return email; }
private boolean sendMailTo(String[] contacts, Poll poll) throws IOException, NoSuchAlgorithmException, SQLException { try { String token; SmtpSender client = new SmtpSender(Parameters.getSMTPServer()); Map<String, String> replacement = new HashMap<String, String>(); replacement.put("emailTitle", poll.getEmailTitle()); replacement.put("sign", poll.getEmailSign()); replacement.put("content", poll.getEmailContent()); for (String contact : contacts) { do { token = Utils.getUrlSafeHash( contact + "-" + poll.getId() + "-" + System.currentTimeMillis() + "-" + Utils.getRandomString(10)); } while (!dao.createToken(contact, poll.getId(), token)); replacement.put("url", Parameters.getFeedbackBaseUrl() + "/#?t=" + token); System.out.println( "[*] Mail to [contact: " + contact + ", pollId: " + poll.getId() + ", token: " + token + "]"); client.sendEmail(createEmail(contact, poll, replacement)); } return true; } catch (MessagingException e) { e.printStackTrace(); return false; } }
public Poll getAnswers(Integer pollId) throws SQLException { Poll poll = get(pollId); List<Answer> answers = dao.getAnswers(pollId); poll.setAnswers(answers.toArray(new Answer[] {})); return poll; }