Example #1
0
  @Override
  protected void sendInternal(
      Map.Entry<String, String> from,
      Map.Entry<String, String> replyTo,
      Map<String, String> to,
      Map<String, String> cc,
      Map<String, String> bcc,
      String subject,
      Object body,
      List<Attachment> attachments) {
    try {
      Session emailSession = getSession();

      Message message = new MimeMessage(emailSession);
      message.setFrom(emailAddress(from));
      if (replyTo != null) {
        message.setReplyTo(new Address[] {emailAddress(replyTo)});
      }

      message.setSubject(subject);

      BasicViewRenderer viewRenderer = render(body);
      String content = viewRenderer.getOutputAsString();
      String contentType = ContentType.cleanContentType(viewRenderer.getContentType());
      contentType = StringUtils.isBlank(contentType) ? ContentType.TextHtml.value() : contentType;

      if (Expressive.isEmpty(attachments)) {
        message.setContent(content, contentType);
      } else {
        Multipart multipart =
            new MimeMultipart(
                "mixed"); // subtype must be "mixed" or inline & regular attachments won't play well
        // together
        addBody(multipart, content, contentType);
        addAttachments(multipart, attachments);
        message.setContent(multipart);
      }

      addRecipients(to, message, RecipientType.TO);
      addRecipients(cc, message, RecipientType.CC);
      addRecipients(bcc, message, RecipientType.BCC);

      sendMessage(message);
    } catch (MessagingException e) {
      throw new MailException(e, "Failed to send an email: %s", e.getMessage());
    }
  }
Example #2
0
 @Override
 public ContentType getContentType() {
   return ContentType.from(getContentTypeString());
 }