Exemplo n.º 1
0
  protected void fillRecipients(Message email, Execution execution, JCRSessionWrapper session)
      throws MessagingException {
    try {
      // to
      AddressTemplate to = getTemplate().getTo();
      if (to != null) {
        fillRecipients(to, email, Message.RecipientType.TO, execution, session);
      }

      // cc
      AddressTemplate cc = getTemplate().getCc();
      if (cc != null) {
        fillRecipients(cc, email, Message.RecipientType.CC, execution, session);
      }

      // bcc
      AddressTemplate bcc = getTemplate().getBcc();
      if (bcc != null) {
        fillRecipients(bcc, email, Message.RecipientType.BCC, execution, session);
      }
    } catch (ScriptException e) {
      logger.error(e.getMessage(), e);
    } catch (RepositoryException e) {
      logger.error(e.getMessage(), e);
    }
  }
Exemplo n.º 2
0
  protected void fillContent(Message email, Execution execution, JCRSessionWrapper session)
      throws MessagingException {
    String text = getTemplate().getText();
    String html = getTemplate().getHtml();
    List<AttachmentTemplate> attachmentTemplates = getTemplate().getAttachmentTemplates();

    try {
      if (html != null || !attachmentTemplates.isEmpty()) {
        // multipart
        MimeMultipart multipart = new MimeMultipart("related");

        BodyPart p = new MimeBodyPart();
        Multipart alternatives = new MimeMultipart("alternative");
        p.setContent(alternatives, "multipart/alternative");
        multipart.addBodyPart(p);

        // html
        if (html != null) {
          BodyPart htmlPart = new MimeBodyPart();
          html = evaluateExpression(execution, html, session);
          htmlPart.setContent(html, "text/html; charset=UTF-8");
          alternatives.addBodyPart(htmlPart);
        }

        // text
        if (text != null) {
          BodyPart textPart = new MimeBodyPart();
          text = evaluateExpression(execution, text, session);
          textPart.setContent(text, "text/plain; charset=UTF-8");
          alternatives.addBodyPart(textPart);
        }

        // attachments
        if (!attachmentTemplates.isEmpty()) {
          addAttachments(execution, multipart);
        }

        email.setContent(multipart);
      } else if (text != null) {
        // unipart
        text = evaluateExpression(execution, text, session);
        email.setText(text);
      }
    } catch (RepositoryException e) {
      logger.error(e.getMessage(), e);
    } catch (ScriptException e) {
      logger.error(e.getMessage(), e);
    }
  }
Exemplo n.º 3
0
 protected void fillSubject(Message email, Execution execution, JCRSessionWrapper session)
     throws MessagingException {
   String subject = getTemplate().getSubject();
   if (subject != null) {
     try {
       String evaluatedSubject =
           evaluateExpression(execution, subject, session).replaceAll("[\r\n]", "");
       email.setSubject(WordUtils.abbreviate(evaluatedSubject, 60, 74, "..."));
     } catch (RepositoryException e) {
       logger.error(e.getMessage(), e);
     } catch (ScriptException e) {
       logger.error(e.getMessage(), e);
     }
   }
 }
Exemplo n.º 4
0
  /**
   * Fills the <code>from</code> attribute of the given email. The sender addresses are an optional
   * element in the mail template. If absent, each mail server supplies the current user's email
   * address.
   *
   * @see {@link InternetAddress#getLocalAddress(Session)}
   */
  protected void fillFrom(Message email, Execution execution, JCRSessionWrapper session)
      throws MessagingException {
    try {
      AddressTemplate fromTemplate = getTemplate().getFrom();
      // "from" attribute is optional
      if (fromTemplate == null) return;

      // resolve and parse addresses
      String addresses = fromTemplate.getAddresses();
      if (addresses != null) {
        addresses = evaluateExpression(execution, addresses, session);
        // non-strict parsing applies to a list of mail addresses entered by a human
        email.addFrom(InternetAddress.parse(addresses, false));
      }

      EnvironmentImpl environment = EnvironmentImpl.getCurrent();
      IdentitySession identitySession = environment.get(IdentitySession.class);
      AddressResolver addressResolver = environment.get(AddressResolver.class);

      // resolve and tokenize users
      String userList = fromTemplate.getUsers();
      if (userList != null) {
        String[] userIds = tokenizeActors(userList, execution, session);
        List<User> users = identitySession.findUsersById(userIds);
        email.addFrom(resolveAddresses(users, addressResolver));
      }

      // resolve and tokenize groups
      String groupList = fromTemplate.getGroups();
      if (groupList != null) {
        for (String groupId : tokenizeActors(groupList, execution, session)) {
          Group group = identitySession.findGroupById(groupId);
          email.addFrom(addressResolver.resolveAddresses(group));
        }
      }
    } catch (ScriptException e) {
      logger.error(e.getMessage(), e);
    } catch (RepositoryException e) {
      logger.error(e.getMessage(), e);
    }
  }