Java Code Examples for javax.mail.Message#isMimeType()

The following examples show how to use javax.mail.Message#isMimeType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MailManager.java    From vpn-over-dns with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public String getMessageContentString(final Message message) throws IOException, MessagingException {
	final Object content = message.getContent();
	if (String.class.isInstance(content))
		// content-type: text/{plain, html, etc.}
		// préférer isMimeType(...) à getContentType().equals(...) car getContentType() peut contenir aussi le charset et renvoyer une string comme suit : << text/html; charset="us-ascii" >>
		if (message.isMimeType("text/html")) return GenericTools.html2Text((String) content);
		else if (message.isMimeType("text/plain")) return (String) content;
		else {
			log.warn("message content-type not handled: " + message.getContentType() + " -> downgrading to String");
			return (String) content;
		}
	else if (MimeMultipart.class.isInstance(content)) {
		boolean mixed = false;
		if (message.isMimeType("multipart/mixed")) mixed = true;
		else if (message.isMimeType("multipart/alternative")) mixed = false;
		else {
			log.warn("multipart content-type not handled: " + message.getContentType() + " -> downgrading to multipart/mixed");
			mixed = true;
		}
		return getMultipartContentString((MimeMultipart) content, mixed);
	}
	else {
		log.warn("invalid message content type and class: " + content.getClass().toString() + " - " + message.getContentType());
		return "";
	}
}
 
Example 2
Source File: MailSteps.java    From NoraUi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @param message
 * @return
 * @throws MessagingException
 * @throws IOException
 */
private static String getTextFromMessage(Message message) throws MessagingException, IOException {
    String result = "";
    if (message.isMimeType("text/plain")) {
        result = message.getContent().toString();
    } else if (message.isMimeType("multipart/*")) {
        final MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
        result = getTextFromMimeMultipart(mimeMultipart);
    }
    return result;
}
 
Example 3
Source File: ReadEmailImap.java    From opentest with MIT License 5 votes vote down vote up
/**
 * Extracts the text content of an email message with support for multipart
 * messages
 */
private String getTextFromMessage(Message message) throws Exception {
    String result = "";
    if (message.isMimeType("multipart/*")) {
        MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
        result = getTextFromMimeMultipart(mimeMultipart);
    } else {
        Object content = message.getContent();
        result = content.toString();
    }

    return result;
}
 
Example 4
Source File: MailReceiverUtils.java    From codenvy with Eclipse Public License 1.0 5 votes vote down vote up
private String getTextFromMessage(Message message) throws MessagingException, IOException {
  if (message.isMimeType("text/plain")) {
    return (String) message.getContent();
  } else if (message.isMimeType("multipart/*")) {
    MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
    return getTextFromMimeMultipart(mimeMultipart);
  } else if (message.isMimeType("text/html")) {
    return (String) message.getContent();
  }

  return "";
}
 
Example 5
Source File: EmailReader.java    From baleen with Apache License 2.0 5 votes vote down vote up
private Boolean hasAttachments(Message msg) throws MessagingException, IOException {
  if (msg.isMimeType("multipart/mixed")) {
    Multipart mp = (Multipart) msg.getContent();
    if (mp.getCount() > 1) {
      return true;
    }
  }

  return false;
}
 
Example 6
Source File: ArdulinkMailMessageCountListener.java    From Ardulink-1 with Apache License 2.0 5 votes vote down vote up
private void validateContentType(Message message) throws MessagingException, IOException {
	boolean isValid = false;
	if(message.isMimeType("text/plain")) { // TODO validare anche text/html
		isValid = true;
	} else if(message.isMimeType("multipart/*")) {
		isValid = isMultipartValid(message);
	}
	
	if(!isValid) {
		throw new MessagingException("MimeType " + message.getContentType() + " is not recognized");
	}
}
 
Example 7
Source File: ArdulinkMailMessageCountAdapter.java    From Ardulink-1 with Apache License 2.0 5 votes vote down vote up
private void validateContentType(Message message) throws MessagingException, IOException {
	boolean isValid = false;
	if(message.isMimeType("text/plain")) { // TODO validare anche text/html
		isValid = true;
	} else if(message.isMimeType("multipart/*")) {
		isValid = isMultipartValid(message);
	}
	
	if(!isValid) {
		throw new MessagingException("MimeType " + message.getContentType() + " is not recognized");
	}
}
 
Example 8
Source File: EmailUtil.java    From product-es with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used to extract verification URL from the e-mail message.
 *
 * @param   message    re-mail message.
 * @return  verification URL to be redirect to.
 */
private static String getBodyFromMessage(Message message) throws IOException, MessagingException {
    if (message.isMimeType("text/plain")) {
        String[] arr = message.getContent().toString().split("\\r?\\n");
        for (int x = 0; x <= arr.length; x++) {
            if (arr[x].contains("https://")) {
                return arr[x];
            }
        }

    }
    return "";
}
 
Example 9
Source File: EmailUtil.java    From product-es with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used to extract verification URL from the e-mail message.
 *
 * @param   message    re-mail message.
 * @return  verification URL to be redirect to.
 */
private static String getBodyFromMessage(Message message) throws IOException, MessagingException {
    if (message.isMimeType("text/plain")) {
        String[] arr = message.getContent().toString().split("\\r?\\n");
        for (int x = 0; x <= arr.length; x++) {
            if (arr[x].contains("https://")) {
                return arr[x];
            }
        }

    }
    return "";
}
 
Example 10
Source File: Mail.java    From camunda-bpm-mail with Apache License 2.0 4 votes vote down vote up
protected static boolean isMultipartMessage(Message message) throws MessagingException, IOException {
  return message.isMimeType("multipart")
      || message.getContent() instanceof Multipart;
}