コード例 #1
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);
    }
  }
コード例 #2
0
  private void fillRecipients(
      AddressTemplate addressTemplate,
      Message email,
      Message.RecipientType recipientType,
      Execution execution,
      JCRSessionWrapper session)
      throws MessagingException, RepositoryException, ScriptException {
    // resolve and parse addresses
    String addresses = addressTemplate.getAddresses();
    if (addresses != null) {
      addresses = evaluateExpression(execution, addresses, session);
      // non-strict parsing applies to a list of mail addresses entered by a human
      email.addRecipients(recipientType, 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 = addressTemplate.getUsers();
    if (userList != null) {
      String[] userIds = tokenizeActors(userList, execution, session);
      List<User> users = identitySession.findUsersById(userIds);
      email.addRecipients(recipientType, resolveAddresses(users, addressResolver));
    }

    // resolve and tokenize groups
    String groupList = addressTemplate.getGroups();
    if (groupList != null) {
      for (String groupId : tokenizeActors(groupList, execution, session)) {
        Group group = identitySession.findGroupById(groupId);
        email.addRecipients(recipientType, addressResolver.resolveAddresses(group));
      }
    }
  }