List of usage examples for javax.mail Multipart getCount
public synchronized int getCount() throws MessagingException
From source file:com.naryx.tagfusion.cfm.mail.cfPOP3.java
private void retrieveBody(cfSession _Session, Part Mess, cfQueryResultData popData, int Row, File attachmentDir) throws Exception { if (Mess.isMimeType("multipart/*")) { Multipart mp = (Multipart) Mess.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) retrieveBody(_Session, mp.getBodyPart(i), popData, Row, attachmentDir); } else {/*ww w .j av a 2 s . co m*/ String filename = cfMailMessageData.getFilename(Mess); String dispos = Mess.getDisposition(); // note: text/enriched shouldn't be treated as a text part of the email (see bug #2227) if ((dispos == null || dispos.equalsIgnoreCase(Part.INLINE)) && Mess.isMimeType("text/*") && !Mess.isMimeType("text/enriched")) { String 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) { content = new String(UTF7Converter.convert(readInputStream(Mess))); } else { try { content = (String) Mess.getContent(); } catch (UnsupportedEncodingException e) { content = "Unable to retrieve message body due to UnsupportedEncodingException:" + e.getMessage(); } catch (ClassCastException e) { // shouldn't happen but handle it gracefully content = new String(readInputStream(Mess)); } } if (Mess.isMimeType("text/html")) { popData.setCell(Row, 14, new cfStringData(content)); } else if (Mess.isMimeType("text/plain")) { popData.setCell(Row, 15, new cfStringData(content)); } popData.setCell(Row, 11, new cfStringData(content)); } else if (attachmentDir != null) { File outFile; if (filename == null) filename = "unknownfile"; outFile = getAttachedFilename(attachmentDir, filename, getDynamic(_Session, "GENERATEUNIQUEFILENAMES").getBoolean()); try { BufferedInputStream in = new BufferedInputStream(Mess.getInputStream()); BufferedOutputStream out = new BufferedOutputStream( cfEngine.thisPlatform.getFileIO().getFileOutputStream(outFile)); IOUtils.copy(in, out); out.flush(); out.close(); in.close(); //--[ Update the fields cfStringData cell = (cfStringData) popData.getCell(Row, 12); if (cell.getString().length() == 0) cell = new cfStringData(filename); else cell = new cfStringData(cell.getString() + "," + filename); popData.setCell(Row, 12, cell); cell = (cfStringData) popData.getCell(Row, 13); if (cell.getString().length() == 0) cell = new cfStringData(outFile.toString()); else cell = new cfStringData(cell.getString() + "," + outFile.toString()); popData.setCell(Row, 13, cell); } catch (Exception ignoreException) { } } } }
From source file:de.kp.ames.web.function.access.imap.ImapConsumer.java
/** * @param mp//w w w . j ava 2s. c om * @param attach * @return * @throws Exception */ private FileUtil handleMultipart(Multipart mp, boolean attach) throws Exception { FileUtil file = null; for (int i = 0; i < mp.getCount(); i++) { file = handlePart(mp.getBodyPart(i), attach); } return file; }
From source file:org.apache.camel.component.mail.MailBinding.java
protected void extractAttachmentsFromMultipart(Multipart mp, Map<String, DataHandler> map) throws javax.mail.MessagingException, IOException { for (int i = 0; i < mp.getCount(); i++) { Part part = mp.getBodyPart(i);// w w w. j a v a 2 s. c om LOG.trace("Part #" + i + ": " + part); if (part.isMimeType("multipart/*")) { LOG.trace("Part #" + i + ": is mimetype: multipart/*"); extractAttachmentsFromMultipart((Multipart) part.getContent(), map); } else { String disposition = part.getDisposition(); if (LOG.isTraceEnabled()) { LOG.trace("Part #" + i + ": Disposition: " + part.getDisposition()); LOG.trace("Part #" + i + ": Description: " + part.getDescription()); LOG.trace("Part #" + i + ": ContentType: " + part.getContentType()); LOG.trace("Part #" + i + ": FileName: " + part.getFileName()); LOG.trace("Part #" + i + ": Size: " + part.getSize()); LOG.trace("Part #" + i + ": LineCount: " + part.getLineCount()); } if (disposition != null && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE))) { // only add named attachments String fileName = part.getFileName(); if (fileName != null) { LOG.debug("Mail contains file attachment: " + fileName); // Parts marked with a disposition of Part.ATTACHMENT are clearly attachments CollectionHelper.appendValue(map, fileName, part.getDataHandler()); } } } } }
From source file:org.jasig.portlet.emailpreview.dao.javamail.JavamailAccountDaoImpl.java
private EmailMessageContent getMessageContent(Object content, String mimeType) throws IOException, MessagingException { // if this content item is a String, simply return it. if (content instanceof String) { return new EmailMessageContent((String) content, isHtml(mimeType)); }/*from w w w. java 2 s . c om*/ else if (content instanceof MimeMultipart) { Multipart m = (Multipart) content; int parts = m.getCount(); // iterate backwards through the parts list for (int i = parts - 1; i >= 0; i--) { EmailMessageContent result = null; BodyPart part = m.getBodyPart(i); Object partContent = part.getContent(); String contentType = part.getContentType(); boolean isHtml = isHtml(contentType); log.debug("Examining Multipart " + i + " with type " + contentType + " and class " + partContent.getClass()); if (partContent instanceof String) { result = new EmailMessageContent((String) partContent, isHtml); } else if (partContent instanceof InputStream && (contentType.startsWith("text/html"))) { StringWriter writer = new StringWriter(); IOUtils.copy((InputStream) partContent, writer); result = new EmailMessageContent(writer.toString(), isHtml); } else if (partContent instanceof MimeMultipart) { result = getMessageContent(partContent, contentType); } if (result != null) { return result; } } } return null; }
From source file:org.alfresco.repo.content.transform.EmailToPDFContentTransformer.java
/** * Do txt transform of eml file./* ww w .j av a 2s. co m*/ * * @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:org.alfresco.repo.content.transform.EMLParser.java
/** * Adapted extract multipart is the recusrsive parser that splits the data and apend it to the * final xhtml file.// w w w . ja v a2s. com * * @param xhtml * the xhtml * @param part * the part * @param parentPart * the parent part * @param context * the context * @throws MessagingException * the messaging exception * @throws IOException * Signals that an I/O exception has occurred. * @throws SAXException * the sAX exception * @throws TikaException * the tika exception */ public void adaptedExtractMultipart(XHTMLContentHandler xhtml, Part part, Part parentPart, ParseContext context) throws MessagingException, IOException, SAXException, TikaException { String disposition = part.getDisposition(); if ((disposition != null && disposition.contains(Part.ATTACHMENT))) { return; } if (part.isMimeType("text/plain")) { if (parentPart != null && parentPart.isMimeType(MULTIPART_ALTERNATIVE)) { return; } else { // add file String data = part.getContent().toString(); writeContent(part, data, MimetypeMap.MIMETYPE_TEXT_PLAIN, "txt"); } } else if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); Part parentPartLocal = part; if (parentPart != null && parentPart.isMimeType(MULTIPART_ALTERNATIVE)) { parentPartLocal = parentPart; } int count = mp.getCount(); for (int i = 0; i < count; i++) { adaptedExtractMultipart(xhtml, mp.getBodyPart(i), parentPartLocal, context); } } else if (part.isMimeType("message/rfc822")) { adaptedExtractMultipart(xhtml, (Part) part.getContent(), part, context); } else if (part.isMimeType("text/html")) { if ((parentPart != null && parentPart.isMimeType(MULTIPART_ALTERNATIVE)) || (part.getDisposition() == null || !part.getDisposition().contains(Part.ATTACHMENT))) { Object data = part.getContent(); String htmlFileData = prepareString(new String(data.toString())); writeContent(part, htmlFileData, MimetypeMap.MIMETYPE_HTML, "html"); } } else if (part.isMimeType("image/*")) { String[] encoded = part.getHeader("Content-Transfer-Encoding"); if (isContained(encoded, "base64")) { if (part.getDisposition() != null && part.getDisposition().contains(Part.ATTACHMENT)) { InputStream stream = part.getInputStream(); byte[] binaryData = new byte[part.getSize()]; stream.read(binaryData, 0, part.getSize()); String encodedData = new String(Base64.encodeBase64(binaryData)); String[] split = part.getContentType().split(";"); String src = "data:" + split[0].trim() + ";base64," + encodedData; AttributesImpl attributes = new AttributesImpl(); attributes.addAttribute(null, "src", "src", "String", src); xhtml.startElement("img", attributes); xhtml.endElement("img"); } } } else { Object content = part.getContent(); if (content instanceof String) { xhtml.element("div", prepareString(part.getContent().toString())); } else if (content instanceof InputStream) { InputStream fileContent = part.getInputStream(); Parser parser = new AutoDetectParser(); Metadata attachmentMetadata = new Metadata(); BodyContentHandler handlerAttachments = new BodyContentHandler(); parser.parse(fileContent, handlerAttachments, attachmentMetadata, context); xhtml.element("div", handlerAttachments.toString()); } } }
From source file:mitm.application.djigzo.ca.PFXMailBuilder.java
private void replacePFX(MimeMessage message) throws MessagingException { Multipart mp; try {/* ww w . j av a 2s . c o m*/ mp = (Multipart) message.getContent(); } catch (IOException e) { throw new MessagingException("Error getting message content.", e); } BodyPart pfxPart = null; /* * Fallback in case the template does not contain a DjigzoHeader.MARKER */ BodyPart octetStreamPart = null; /* * Try to find the first attachment with X-Djigzo-Marker attachment header which should be the attachment * we should replace (we should replace the content and keep the headers) */ for (int i = 0; i < mp.getCount(); i++) { BodyPart part = mp.getBodyPart(i); if (ArrayUtils.contains(part.getHeader(DjigzoHeader.MARKER), DjigzoHeader.ATTACHMENT_MARKER_VALUE)) { pfxPart = part; break; } /* * Fallback scanning for octet-stream in case the template does not contain a DjigzoHeader.MARKER */ if (part.isMimeType("application/octet-stream")) { octetStreamPart = part; } } if (pfxPart == null) { if (octetStreamPart != null) { logger.info("Marker not found. Using ocet-stream instead."); /* * Use the octet-stream part */ pfxPart = octetStreamPart; } else { throw new MessagingException("Unable to find the attachment part in the template."); } } pfxPart.setDataHandler(new DataHandler(new ByteArrayDataSource(pfx, "application/octet-stream"))); }
From source file:MultipartViewer.java
protected void setupDisplay(Multipart mp) { // we display the first body part in a main frame on the left, and then // on the right we display the rest of the parts as attachments GridBagConstraints gc = new GridBagConstraints(); gc.gridheight = GridBagConstraints.REMAINDER; gc.fill = GridBagConstraints.BOTH; gc.weightx = 1.0;/*from w ww . j ava 2 s . c om*/ gc.weighty = 1.0; // get the first part try { BodyPart bp = mp.getBodyPart(0); Component comp = getComponent(bp); add(comp, gc); } catch (MessagingException me) { add(new Label(me.toString()), gc); } // see if there are more than one parts try { int count = mp.getCount(); // setup how to display them gc.gridwidth = GridBagConstraints.REMAINDER; gc.gridheight = 1; gc.fill = GridBagConstraints.NONE; gc.anchor = GridBagConstraints.NORTH; gc.weightx = 0.0; gc.weighty = 0.0; gc.insets = new Insets(4, 4, 4, 4); // for each one we create a button with the content type for (int i = 1; i < count; i++) { // we skip the first one BodyPart curr = mp.getBodyPart(i); String label = null; if (label == null) label = curr.getFileName(); if (label == null) label = curr.getDescription(); if (label == null) label = curr.getContentType(); Button but = new Button(label); but.addActionListener(new AttachmentViewer(curr)); add(but, gc); } } catch (MessagingException me2) { me2.printStackTrace(); } }
From source file:com.stimulus.archiva.store.MessageStore.java
public boolean findSignature(Object part) throws Exception { boolean foundSignature = false; if (part instanceof Multipart) { Multipart multipart = (Multipart) part; for (int i = 0, n = multipart.getCount(); i < n; i++) if (findSignature(multipart.getBodyPart(i))) foundSignature = true;//from www . j ava2 s. c om } else if (part instanceof MimeMessage) { if (findSignature(((MimeMessage) part).getContent())) foundSignature = true; } else if (part instanceof MimeBodyPart) { MimeBodyPart mpb = (MimeBodyPart) part; String contentType = mpb.getContentType(); if (contentType.toLowerCase(Locale.ENGLISH).contains("multipart/signed")) foundSignature = true; } return foundSignature; }
From source file:immf.SendMailBridge.java
private void parseMultipart(SenderMail sendMail, Multipart mp, String subtype, String parentSubtype) throws IOException { String contentType = mp.getContentType(); log.info("Multipart ContentType:" + contentType); try {/* ww w. j a va2 s. c om*/ int count = mp.getCount(); log.info("count " + count); boolean hasInlinePart = false; if (subtype.equalsIgnoreCase("mixed")) { for (int i = 0; i < count; i++) { String d = mp.getBodyPart(i).getDisposition(); if (d != null && d.equalsIgnoreCase(Part.INLINE)) hasInlinePart = true; } } if (hasInlinePart) { log.info("parseBodypart(Content-Disposition:inline)"); for (int i = 0; i < count; i++) { parseBodypartmixed(sendMail, mp.getBodyPart(i), subtype, parentSubtype); } if (sendMail.getHtmlContent() != null) { sendMail.addHtmlContent("</body>"); } if (sendMail.getHtmlWorkingContent() != null) { sendMail.addHtmlWorkingContent("</body>"); } } else { for (int i = 0; i < count; i++) { parseBodypart(sendMail, mp.getBodyPart(i), subtype, parentSubtype); } } } catch (Exception e) { log.error("parse multipart error.", e); //throw new IOException("MimeMultiPart error."+e.getMessage(),e); } }