예제 #1
0
  public String execute() {
    // Prepare list of recipients
    List<BusinessAssociate> businessAssociates = populateBAFromRecipients(to);

    if (!businessAssociates.isEmpty()) {
      for (final BusinessAssociate businessAssociate : businessAssociates) {

        // Create message preparator
        MimeMessagePreparator messagePreparator =
            new MimeMessagePreparator() {

              public void prepare(MimeMessage mimeMessage) throws Exception {
                // Use the true flag to indicate you need a multipart message
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

                // Merge the model into the freemarker template
                String result;
                Map<String, String> model = null;

                if (!templateName.isEmpty()) {
                  try {
                    // Add stuff to the model for inserting into the template
                    model = new HashMap<String, String>();
                    model.put(
                        "name",
                        businessAssociate.getFirstName() + " " + businessAssociate.getLastName());
                    model.put("inlineImage", "<img src='cid:inlineImage'>");

                    result =
                        FreeMarkerTemplateUtils.processTemplateIntoString(
                            fmConfig.getTemplate(templateName), model);
                  } catch (IOException e) {
                    throw new SendException(
                        "Unable to read or process freemarker configuration or template", e);
                  } catch (TemplateException e) {
                    throw new SendException(
                        "Problem initializing freemarker or rendering template '"
                            + templateName
                            + "'",
                        e);
                  }
                } else {
                  result = message;
                }

                helper.setFrom(from);
                helper.setTo(businessAssociate.getEmail());
                helper.setSubject(subject);

                // Use the true flag to indicate the text included is HTML
                helper.setText(result, true);

                mailType = mailTypeDao.findByDescription(templateName);
                if (mailType != null) {
                  LetterTemplate template = mailType.getLetterTemplate();
                  if (template != null) {
                    // Add an inline image
                    helper.addInline(
                        "inlineImage",
                        new ByteArrayResource(template.getTemplate()),
                        template.getContentType());
                  }
                }
              }
            };

        try {
          sendMail.send(messagePreparator);
          addActionMessage("Message was sent.");
          return SUCCESS;
        } catch (Exception e) {
          e.printStackTrace();
          return INPUT;
        }
      }
    }

    return INPUT;
  }
예제 #2
0
 public void prepare() {
   // Authenticate current client
   client = (Client) session.get(Constants.CLIENT);
   sendMail.authenticate(client);
 }