public static String getTextContent(Part part, String type) { if (part == null) return null; try { String contentType; try { contentType = part.getContentType(); } catch (Throwable t) { contentType = "unknown"; } if (contentType.toLowerCase().startsWith("text/" + type)) { // ContentType ct = new ContentType(contentType); // String charset = ct.getParameter("charset"); try { Object content = part.getContent(); if (content == null) return null; if (content instanceof String) return (String) content; if (content instanceof InputStream) { String encoding = charset; if (contentType.toLowerCase().contains("UTF")) encoding = IO.UTF_8; if (contentType.toLowerCase().contains("ISO")) encoding = IO.ISO_LATIN_1; return IO.readToString((InputStream) content, encoding); } return Utl.toStringWithType(content); } catch (UnsupportedEncodingException ex) { LOG.warn(ex); return null; } catch (IOException e) { String message = e.getMessage(); if (message != null) { if ("No content".equals(message)) { return null; } if (message.toLowerCase().startsWith("unknown encoding")) { LOG.warn(e); return null; } } throw e; } catch (Throwable t) { LOG.warn(t); return Str.getStackTrace(t); } } if (contentType.toLowerCase().startsWith("multipart")) { MimeMultipart multipart; try { multipart = (MimeMultipart) part.getContent(); } catch (NullPointerException ex) { LOG.warn(ex); return null; } int count = multipart.getCount(); for (int i = 0; i < count; i++) { BodyPart subPart = multipart.getBodyPart(i); String filename = subPart.getFileName(); if (filename != null) continue; String text = getTextContent(subPart, type); if (text != null) return text.trim(); } return null; } return null; } catch (Exception ex) { throw new RuntimeException(ex); } }