Example #1
0
 public static String getContentAsText(Part part) {
   String result = getPlainTextContent(part);
   if (result == null) {
     result = Html.convertHtmlToText(getHtmlTextContent(part));
   } else {
     if (result != null && result.trim().startsWith("<!DOCTYPE HTML"))
       result = Html.convertHtmlToText(result);
   }
   return result;
 }
Example #2
0
  public static MimeMessage createMessage(
      Session session,
      String subject,
      String html,
      String text,
      Address from,
      Address[] to,
      Attachment... attachments) {

    if (text == null) text = Html.convertHtmlToText(html);

    MimeMessage msg = createEmptyMimeMessage(session);
    try {
      msg.setSubject(subject, charset);
      msg.setFrom(from);
      msg.setRecipients(Message.RecipientType.TO, to);

      if ((attachments == null || attachments.length == 0)) {
        // no attachments

        if (Str.isBlank(html)) {
          // no html
          msg.setText(text, charset);
          return msg;
        }

        msg.setContent(createMultipartAlternative(text, html));
        return msg;
      }

      MimeMultipart multipartMixed = new MimeMultipart("mixed");

      if (Str.isBlank(html)) {
        // no html
        MimeBodyPart textBodyPart = new MimeBodyPart();
        textBodyPart.setText(text, charset);
        multipartMixed.addBodyPart(textBodyPart);
      } else {
        // html
        MimeBodyPart wrapAlternative = new MimeBodyPart();
        wrapAlternative.setContent(createMultipartAlternative(text, html));
        multipartMixed.addBodyPart(wrapAlternative);
      }

      for (Attachment attachment : attachments) {
        appendAttachment(multipartMixed, attachment);
      }

      msg.setContent(multipartMixed);
      return msg;

    } catch (MessagingException ex) {
      throw new RuntimeException(ex);
    }
  }