List of usage examples for javax.mail Part getContent
public Object getContent() throws IOException, MessagingException;
From source file:com.ikon.util.MailUtils.java
/** * Add attachments to an imported mail.//from ww w. j a v a2 s .c o m */ public static void addAttachments(String token, com.ikon.bean.Mail mail, Part p, String userId) throws MessagingException, IOException, UnsupportedMimeTypeException, FileSizeExceededException, UserQuotaExceededException, VirusDetectedException, ItemExistsException, PathNotFoundException, AccessDeniedException, RepositoryException, DatabaseException, ExtensionException, AutomationException { if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); int count = mp.getCount(); for (int i = 1; i < count; i++) { BodyPart bp = mp.getBodyPart(i); if (bp.getFileName() != null) { String name = MimeUtility.decodeText(bp.getFileName()); String fileName = FileUtils.getFileName(name); String fileExtension = FileUtils.getFileExtension(name); String testName = name; // Test if already exists a document with the same name in the mail for (int j = 1; OKMRepository.getInstance().hasNode(token, mail.getPath() + "/" + testName); j++) { // log.info("Trying with: {}", testName); testName = fileName + " (" + j + ")." + fileExtension; } Document attachment = new Document(); String mimeType = MimeTypeConfig.mimeTypes.getContentType(bp.getFileName().toLowerCase()); attachment.setMimeType(mimeType); attachment.setPath(mail.getPath() + "/" + testName); InputStream is = bp.getInputStream(); if (Config.REPOSITORY_NATIVE) { new DbDocumentModule().create(token, attachment, is, bp.getSize(), userId); } else { new JcrDocumentModule().create(token, attachment, is, userId); } is.close(); } } } }
From source file:com.riq.MailHandlerServlet.java
private String getText(Part p) throws MessagingException, IOException { // log.info("Message S Zero"); // log.info("Inside MailServlet getText"); if (p.isMimeType("text/*")) { String s = (String) p.getContent(); log.info("Message S1: " + s); boolean textIsHtml = p.isMimeType("text/html"); return s; }/*w w w. j a va 2 s . c o m*/ if (p.isMimeType("multipart/alternative")) { Multipart mp = (Multipart) p.getContent(); String text = null; for (int i = 0; i < mp.getCount(); i++) { Part bp = mp.getBodyPart(i); if (bp.isMimeType("text/plain")) { if (text == null) text = getText(bp); log.info("Message S2: " + text); continue; } else if (bp.isMimeType("text/html")) { String s = getText(bp); if (s != null) log.info("Message S3: " + s); return s; } else if (bp.isMimeType("message/rfc822")) { Object nestedObject = ""; nestedObject = bp.getContent(); Multipart nestedPart = (Multipart) nestedObject; BodyPart nestedBodyPart = nestedPart.getBodyPart(0); String s = getText(nestedBodyPart); if (s != null) log.info("Message Nested: " + s); return s; } else { log.info("Message nada"); return getText(bp); } } return text; } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) { String s = getText(mp.getBodyPart(i)); if (s != null) { log.info("Message S4: " + s); return s; } } } return null; }
From source file:org.alfresco.repo.content.transform.EmailToPDFContentTransformer.java
/** * Do txt transform of eml file./*from ww w . j a va2s . c om*/ * * @param is * the input stream * @param os * the final output stream * @param inputMime * the input mime type * @param targetMimeType * the target mime type * @param encoding * the encoding of reader * @param writerEncoding * the writer encoding * @throws IOException * Signals that an I/O exception has occurred. * @throws TransformerConfigurationException * the transformer configuration exception * @throws SAXException * the sAX exception * @throws TikaException * the tika exception * @throws MessagingException * the messaging exception */ protected void doTxtTransform(InputStream is, OutputStream os, String inputMime, String targetMimeType, String encoding, String writerEncoding) throws IOException, TransformerConfigurationException, SAXException, TikaException, MessagingException { MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()), is); final StringBuilder sb = new StringBuilder(); Object content = mimeMessage.getContent(); if (content instanceof Multipart) { Multipart multipart = (Multipart) content; Part part = multipart.getBodyPart(0); if (part.getContent() instanceof Multipart) { multipart = (Multipart) part.getContent(); for (int i = 0, n = multipart.getCount(); i < n; i++) { part = multipart.getBodyPart(i); if (part.isMimeType("text/*")) { sb.append(part.getContent().toString()).append("\n"); } } } else if (part.isMimeType("text/*")) { sb.append(part.getContent().toString()); } } else { sb.append(content.toString()); } textToPDF(new ByteArrayInputStream(sb.toString().getBytes()), UTF_8, os); }
From source file:com.naryx.tagfusion.cfm.mail.cfMailMessageData.java
private void extractBody(Part Mess, cfArrayData AD, String attachURI, String attachDIR) throws Exception { if (Mess.isMimeType("multipart/*")) { Multipart mp = (Multipart) Mess.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) extractBody(mp.getBodyPart(i), AD, attachURI, attachDIR); } else {//from w w w . j av a 2 s .com cfStructData sd = new cfStructData(); String tmp = Mess.getContentType(); if (tmp.indexOf(";") != -1) tmp = tmp.substring(0, tmp.indexOf(";")); sd.setData("mimetype", new cfStringData(tmp)); String filename = getFilename(Mess); String dispos = getDisposition(Mess); MimeType messtype = new MimeType(tmp); // Note that we can't use Mess.isMimeType() here due to bug #2080 if ((dispos == null || dispos.equalsIgnoreCase(Part.INLINE)) && messtype.match("text/*")) { Object content; String contentType = Mess.getContentType().toLowerCase(); // support aliases of UTF-7 - UTF7, UNICODE-1-1-UTF-7, csUnicode11UTF7, UNICODE-2-0-UTF-7 if (contentType.indexOf("utf-7") != -1 || contentType.indexOf("utf7") != -1) { InputStream ins = Mess.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(ins, bos); content = new String(UTF7Converter.convert(bos.toByteArray())); } else { try { content = Mess.getContent(); } catch (UnsupportedEncodingException e) { content = Mess.getInputStream(); } catch (IOException ioe) { // This will happen on BD/Java when the attachment has no content // NOTE: this is the fix for bug NA#3198. content = ""; } } if (content instanceof InputStream) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy((InputStream) content, bos); sd.setData("content", new cfStringData(new String(bos.toByteArray()))); } else { sd.setData("content", new cfStringData(content.toString())); } sd.setData("file", cfBooleanData.FALSE); sd.setData("filename", new cfStringData(filename == null ? "" : filename)); } else if (attachDIR != null) { sd.setData("content", new cfStringData("")); if (filename == null || filename.length() == 0) filename = "unknownfile"; filename = getAttachedFilename(attachDIR, filename); //--[ An attachment, save it out to disk try { BufferedInputStream in = new BufferedInputStream(Mess.getInputStream()); BufferedOutputStream out = new BufferedOutputStream( cfEngine.thisPlatform.getFileIO().getFileOutputStream(new File(attachDIR + filename))); IOUtils.copy(in, out); out.flush(); out.close(); in.close(); sd.setData("file", cfBooleanData.TRUE); sd.setData("filename", new cfStringData(filename)); if (attachURI.charAt(attachURI.length() - 1) != '/') sd.setData("url", new cfStringData(attachURI + '/' + filename)); else sd.setData("url", new cfStringData(attachURI + filename)); sd.setData("size", new cfNumberData((int) new File(attachDIR + filename).length())); } catch (Exception ignoreException) { // NOTE: this could happen when we don't have permission to write to the specified directory // so let's log an error message to make this easier to debug. cfEngine.log("-] Failed to save attachment to " + attachDIR + filename + ", exception=[" + ignoreException.toString() + "]"); } } else { sd.setData("file", cfBooleanData.FALSE); sd.setData("content", new cfStringData("")); } AD.addElement(sd); } }
From source file:it.greenvulcano.gvesb.virtual.http.HTTPCallOperation.java
private void dumpPart(Part p, Element msg, Document doc) throws Exception { Element content = null;//from w w w. j a v a 2 s . c o m if (p.isMimeType("text/plain")) { content = doc.createElement("PlainMessage"); Text body = doc.createTextNode((String) p.getContent()); content.appendChild(body); } else if (p.isMimeType("text/html")) { content = doc.createElement("HTMLMessage"); CDATASection body = doc.createCDATASection((String) p.getContent()); content.appendChild(body); } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); int count = mp.getCount(); content = doc.createElement("Multipart"); for (int i = 0; i < count; i++) { dumpPart(mp.getBodyPart(i), content, doc); } } else if (p.isMimeType("message/rfc822")) { content = doc.createElement("NestedMessage"); dumpPart((Part) p.getContent(), content, doc); } else { content = doc.createElement("EncodedContent"); DataHandler dh = p.getDataHandler(); OutputStream os = new ByteArrayOutputStream(); Base64EncodingOutputStream b64os = new Base64EncodingOutputStream(os); dh.writeTo(b64os); b64os.flush(); b64os.close(); content.appendChild(doc.createTextNode(os.toString())); } msg.appendChild(content); String filename = p.getFileName(); if (filename != null) { content.setAttribute("file-name", filename); } String ct = p.getContentType(); if (ct != null) { content.setAttribute("content-type", ct); } String desc = p.getDescription(); if (desc != null) { content.setAttribute("description", desc); } }
From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java
public void buildMd5CheckSums(List<String> checksums, Part part) throws MessagingException, IOException { if (part.isMimeType(MULTIPART_TYPE)) { Multipart mp = (Multipart) part.getContent(); for (int i = 0; i < mp.getCount(); i++) { buildMd5CheckSums(checksums, mp.getBodyPart(i)); }/* w w w.j ava 2s . co m*/ } else if (isAttach(part)) { checksums.add(getCheckSum(part)); } }
From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java
public boolean hasAttach(Part part) throws MessagingException, IOException { if (part.isMimeType(MULTIPART_TYPE)) { Multipart mp = (Multipart) part.getContent(); boolean has = false; for (int i = 0; i < mp.getCount(); i++) { if (hasAttach(mp.getBodyPart(i))) { has = true;/*ww w . j a v a 2 s . c o m*/ break; } } return has; } else if (isAttach(part)) { return true; } else { return false; } }
From source file:org.apache.hupa.server.handler.GetMessageDetailsHandler.java
/** * Handle the parts of the given message. The method will call itself recursively to handle all nested parts * @param message the MimeMessage /* w ww . ja v a 2 s .com*/ * @param con the current processing Content * @param sbPlain the StringBuffer to fill with text * @param attachmentList ArrayList with attachments * @throws UnsupportedEncodingException * @throws MessagingException * @throws IOException */ protected boolean handleParts(MimeMessage message, Object con, StringBuffer sbPlain, ArrayList<MessageAttachment> attachmentList) throws UnsupportedEncodingException, MessagingException, IOException { boolean isHTML = false; if (con instanceof String) { if (message.getContentType().toLowerCase().startsWith("text/html")) { isHTML = true; } else { isHTML = false; } sbPlain.append((String) con); } else if (con instanceof Multipart) { Multipart mp = (Multipart) con; String multipartContentType = mp.getContentType().toLowerCase(); String text = null; if (multipartContentType.startsWith("multipart/alternative")) { isHTML = handleMultiPartAlternative(mp, sbPlain); } else { for (int i = 0; i < mp.getCount(); i++) { Part part = mp.getBodyPart(i); String contentType = part.getContentType().toLowerCase(); Boolean bodyRead = sbPlain.length() > 0; if (!bodyRead && contentType.startsWith("text/plain")) { isHTML = false; text = (String) part.getContent(); } else if (!bodyRead && contentType.startsWith("text/html")) { isHTML = true; text = (String) part.getContent(); } else if (!bodyRead && contentType.startsWith("message/rfc822")) { // Extract the message and pass it MimeMessage msg = (MimeMessage) part.getDataHandler().getContent(); isHTML = handleParts(msg, msg.getContent(), sbPlain, attachmentList); } else { if (part.getFileName() != null) { // Inline images are not added to the attachment list // TODO: improve the in-line images detection if (part.getHeader("Content-ID") == null) { MessageAttachment attachment = new MessageAttachment(); attachment.setName(MimeUtility.decodeText(part.getFileName())); attachment.setContentType(part.getContentType()); attachment.setSize(part.getSize()); attachmentList.add(attachment); } } else { isHTML = handleParts(message, part.getContent(), sbPlain, attachmentList); } } } if (text != null) sbPlain.append(text); } } return isHTML; }
From source file:msgshow.java
public static void dumpPart(Part p) throws Exception { if (p instanceof Message) dumpEnvelope((Message) p);// ww w . j a va2s . co m /** Dump input stream .. InputStream is = p.getInputStream(); // If "is" is not already buffered, wrap a BufferedInputStream // around it. if (!(is instanceof BufferedInputStream)) is = new BufferedInputStream(is); int c; while ((c = is.read()) != -1) System.out.write(c); **/ String ct = p.getContentType(); try { pr("CONTENT-TYPE: " + (new ContentType(ct)).toString()); } catch (ParseException pex) { pr("BAD CONTENT-TYPE: " + ct); } String filename = p.getFileName(); if (filename != null) pr("FILENAME: " + filename); /* * Using isMimeType to determine the content type avoids * fetching the actual content data until we need it. */ if (p.isMimeType("text/plain")) { pr("This is plain text"); pr("---------------------------"); if (!showStructure && !saveAttachments) System.out.println((String) p.getContent()); } else if (p.isMimeType("multipart/*")) { pr("This is a Multipart"); pr("---------------------------"); Multipart mp = (Multipart) p.getContent(); level++; int count = mp.getCount(); for (int i = 0; i < count; i++) dumpPart(mp.getBodyPart(i)); level--; } else if (p.isMimeType("message/rfc822")) { pr("This is a Nested Message"); pr("---------------------------"); level++; dumpPart((Part) p.getContent()); level--; } else { if (!showStructure && !saveAttachments) { /* * If we actually want to see the data, and it's not a * MIME type we know, fetch it and check its Java type. */ Object o = p.getContent(); if (o instanceof String) { pr("This is a string"); pr("---------------------------"); System.out.println((String) o); } else if (o instanceof InputStream) { pr("This is just an input stream"); pr("---------------------------"); InputStream is = (InputStream) o; int c; while ((c = is.read()) != -1) System.out.write(c); } else { pr("This is an unknown type"); pr("---------------------------"); pr(o.toString()); } } else { // just a separator pr("---------------------------"); } } /* * If we're saving attachments, write out anything that * looks like an attachment into an appropriately named * file. Don't overwrite existing files to prevent * mistakes. */ if (saveAttachments && level != 0 && p instanceof MimeBodyPart && !p.isMimeType("multipart/*")) { String disp = p.getDisposition(); // many mailers don't include a Content-Disposition if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) { if (filename == null) filename = "Attachment" + attnum++; pr("Saving attachment to file " + filename); try { File f = new File(filename); if (f.exists()) // XXX - could try a series of names throw new IOException("file exists"); ((MimeBodyPart) p).saveFile(f); } catch (IOException ex) { pr("Failed to save attachment: " + ex); } pr("---------------------------"); } } }
From source file:MainClass.java
public static void dumpPart(Part p) throws Exception { if (p instanceof Message) dumpEnvelope((Message) p);// w w w. java 2 s . com /** * Dump input stream .. * * InputStream is = p.getInputStream(); // If "is" is not already buffered, * wrap a BufferedInputStream // around it. if (!(is instanceof * BufferedInputStream)) is = new BufferedInputStream(is); int c; while ((c = * is.read()) != -1) System.out.write(c); * */ String ct = p.getContentType(); try { pr("CONTENT-TYPE: " + (new ContentType(ct)).toString()); } catch (ParseException pex) { pr("BAD CONTENT-TYPE: " + ct); } String filename = p.getFileName(); if (filename != null) pr("FILENAME: " + filename); /* * Using isMimeType to determine the content type avoids fetching the actual * content data until we need it. */ if (p.isMimeType("text/plain")) { pr("This is plain text"); pr("---------------------------"); if (!showStructure && !saveAttachments) System.out.println((String) p.getContent()); } else if (p.isMimeType("multipart/*")) { pr("This is a Multipart"); pr("---------------------------"); Multipart mp = (Multipart) p.getContent(); level++; int count = mp.getCount(); for (int i = 0; i < count; i++) dumpPart(mp.getBodyPart(i)); level--; } else if (p.isMimeType("message/rfc822")) { pr("This is a Nested Message"); pr("---------------------------"); level++; dumpPart((Part) p.getContent()); level--; } else { if (!showStructure && !saveAttachments) { /* * If we actually want to see the data, and it's not a MIME type we * know, fetch it and check its Java type. */ Object o = p.getContent(); if (o instanceof String) { pr("This is a string"); pr("---------------------------"); System.out.println((String) o); } else if (o instanceof InputStream) { pr("This is just an input stream"); pr("---------------------------"); InputStream is = (InputStream) o; int c; while ((c = is.read()) != -1) System.out.write(c); } else { pr("This is an unknown type"); pr("---------------------------"); pr(o.toString()); } } else { // just a separator pr("---------------------------"); } } /* * If we're saving attachments, write out anything that looks like an * attachment into an appropriately named file. Don't overwrite existing * files to prevent mistakes. */ if (saveAttachments && level != 0 && !p.isMimeType("multipart/*")) { String disp = p.getDisposition(); // many mailers don't include a Content-Disposition if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) { if (filename == null) filename = "Attachment" + attnum++; pr("Saving attachment to file " + filename); try { File f = new File(filename); if (f.exists()) // XXX - could try a series of names throw new IOException("file exists"); ((MimeBodyPart) p).saveFile(f); } catch (IOException ex) { pr("Failed to save attachment: " + ex); } pr("---------------------------"); } } }