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); } }
/** * 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); } }
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); } } }
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)); } } }