protected static String readContent(Part part) throws IOException, MessagingException { return new String(part.getContent().toString().getBytes(), readCharset(part.getContentType())); } /** * Read charset from ContentType of javax.mail.internet.Part (MimePart, BodyPart, etc). * eg: "text/plain; charset=iso-8859-1" * The result: Charset ISO-8859-1 * * @param contentType content-type like "text/plain; charset=iso-8859-1" * @return Charset */ protected static Charset readCharset(String contentType) { Charset charset = Charset.defaultCharset(); if (StringUtils.hasText(contentType)) { String[] _split = contentType.split("charset="); if (_split != null && _split.length == 2) { String _c = _split[1]; try { charset = Charset.forName(_c); } catch (Exception ex) { log.error("Invalid content-type charset: " + contentType); } } } return charset; }
MimeMultipart mp = (MimeMultipart) attachment.getContent(); for (int i = 0; i < mp.getCount(); i++) { Part bp = mp.getBodyPart(i); if (bp.isMimeType("text/*")) { String text = (String)bp.getContent(); //process text } else if (bp.isMimeType("image/*")) { InputStream is = bp.getInputStream(); //process image } }
Object content = messagePart.getContent();
Object objContent = p.getContent(); if(objContent instanceof String){ if(p.isMimeType("text/html")){ Multipart mp = (Multipart) p.getContent(); String text = null; for (int i = 0; i < mp.getCount(); i++) { Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) { String s = getText(mp.getBodyPart(i),embededImgMap); Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) { String s = getText(mp.getBodyPart(i),embededImgMap);
private String getText(Part p) throws MessagingException, IOException { System.out.println("getText contentType "+p.getContentType()); if (p.isMimeType("text/plain")) { String s = (String) p.getContent(); System.out.println(s); return s; } else if (p.isMimeType("text/html")) { // the last two parameters of this may need to be modified String s = Jsoup.parse(p.getInputStream(), null, null).text(); System.out.println(s); return s; } else if (p.isMimeType("multipart/alternative")) { Multipart mp = (Multipart) p.getContent(); String text = ""; for (int i = 0; i < mp.getCount(); i++) { Part bp = mp.getBodyPart(i); if (bp.isMimeType("text/*")) { String s = getText(bp); if (s != null) { text += s; } } } return text; } return null; }