Exemplo n.º 1
0
  public OkResponse contact(String alias, MailToShow mailToSend) {
    ValidationResult validate = captchaValidator.validate(mailToSend.getCaptcha());
    if (!validate.isSuccess()) {
      throw new IllegalArgumentException("Rosszul megadott Captcha: " + validate.toString());
    }
    MimeMessage mail = mailSender.createMimeMessage();
    try {
      MimeMessageHelper helper = new MimeMessageHelper(mail, false);

      String body =
          "----- Ez a levél a tilos.hu műsoroldaláról lett küldve-----\n"
              + "\n"
              + "A form kitöltője a "
              + mailToSend.getFrom()
              + " email-t adta meg válasz címnek, de ennek valódiságát nem ellenőriztük."
              + "\n"
              + "-------------------------------------"
              + "\n"
              + mailToSend.getBody();

      helper.setFrom("*****@*****.**");
      helper.setReplyTo(mailToSend.getFrom());
      helper.setSubject("[tilos.hu] " + mailToSend.getSubject());
      helper.setText(body);

      DBObject one = db.getCollection("show").findOne(aliasOrId(alias));
      if (one == null) {
        throw new IllegalArgumentException("No such show: " + alias);
      }
      ShowDetailed detailed = mapper.map(one, ShowDetailed.class);

      detailed
          .getContributors()
          .forEach(
              contributor -> {
                DBObject dbAuthor =
                    db.getCollection("author").findOne(aliasOrId(contributor.getAuthor().getId()));

                if (dbAuthor.get("email") != null) {
                  try {
                    helper.setTo((String) dbAuthor.get("email"));
                  } catch (MessagingException e) {
                    throw new RuntimeException(e);
                  }
                  mailSender.send(mail);
                }
              });
    } catch (Exception e) {
      throw new InternalErrorException("Can't send the email message: " + e.getMessage(), e);
    }
    return new OkResponse("Üzenet elküldve.");
  }
  @Override
  public void publishNotification(final Notification notification) throws PublicationException {
    log.debug(
        "Send mail notification '{}' to {}", notification.getCode(), notification.getRecipient());
    final MimeMessagePreparator mm =
        mimeMessage -> {
          final Language language = Language.RU;
          final MimeMessageHelper msg = new MimeMessageHelper(mimeMessage, false, "UTF-8");

          msg.setSubject(notification.getSubject());
          msg.setFrom(getInternetAddress(notification.getSender(), language));
          msg.setTo(getInternetAddress(notification.getRecipient(), language));

          msg.setBcc(getInternetAddress(Recipient.get(Recipient.MailBox.MONITORING), language));

          final Recipient recipient = notification.getRecipient();
          if (recipient instanceof Recipient.Application) {
            final Recipient.Application application = (Recipient.Application) recipient;
            if (application.getReturnAddress() != null) {
              msg.setReplyTo(getInternetAddress(application.getReturnAddress(), language));
            }
            msg.setText(notification.getMessage(), true);
          } else if (recipient instanceof Recipient.Person) {
            msg.setReplyTo(getInternetAddress(Recipient.get(Recipient.MailBox.SUPPORT), language));

            msg.setText(notification.getMessage(), true);
          } else {
            log.error("There is not processor for recipient {}", recipient);
          }
        };
    try {
      mailSender.send(mm);
    } catch (MailException ex) {
      throw new PublicationException(ex);
    }
  }