List of usage examples for javax.mail Multipart getCount
public synchronized int getCount() throws MessagingException
From source file:ru.codemine.ccms.mail.EmailAttachmentExtractor.java
public void saveAllAttachment() { String protocol = ssl ? "imaps" : "imap"; Properties props = new Properties(); props.put("mail.store.protocol", protocol); try {//from ww w . j av a 2 s .c om Session session = Session.getInstance(props); Store store = session.getStore(); store.connect(host, port, username, password); Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_WRITE); for (Message msg : inbox.getMessages()) { if (!msg.isSet(Flags.Flag.SEEN)) { String fileNamePrefix = settingsService.getStorageEmailPath() + "msg-" + msg.getMessageNumber(); Multipart multipart = (Multipart) msg.getContent(); for (int i = 0; i < multipart.getCount(); i++) { BodyPart bodyPart = multipart.getBodyPart(i); if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) && StringUtils.isNotEmpty(bodyPart.getFileName())) { MimeBodyPart mimimi = (MimeBodyPart) bodyPart; // =(^.^)= mimimi.saveFile(fileNamePrefix + MimeUtility.decodeText(bodyPart.getFileName())); msg.setFlag(Flags.Flag.SEEN, true); } } } } } catch (IOException | MessagingException e) { log.error("? ? ?, : " + e.getMessage()); } }
From source file:com.ikon.util.MailUtils.java
/** * Add attachments to an imported mail.//ww w.j av a 2 s . c om */ 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:org.apache.synapse.transport.mail.MailUtils.java
public InputStream getInputStream(Object message) { try {/*from w ww . ja va 2 s .co m*/ if (message instanceof MimeMessage) { MimeMessage msg = (MimeMessage) message; if (msg.getContent() instanceof Multipart) { MimeBodyPart firstTextPart = null; Multipart mp = (Multipart) msg.getContent(); for (int i = 0; i < mp.getCount(); i++) { MimeBodyPart mbp = (MimeBodyPart) mp.getBodyPart(i); String contType = mbp.getContentType(); if (contType != null && (contType.indexOf(SOAP11Constants.SOAP_11_CONTENT_TYPE) != -1 || contType.indexOf(SOAP12Constants.SOAP_12_CONTENT_TYPE) != -1)) { // this part is a SOAP 11 or 12 payload, treat this as the message return mbp.getInputStream(); } else if (mbp == null && contType.indexOf("plain/text") != -1) { firstTextPart = mbp; } } // if a soap 11 or soap12 payload was not found, treat first text part as message return firstTextPart.getInputStream(); } else { return ((Message) message).getInputStream(); } } } catch (Exception e) { handleException("Error creating an input stream to : " + ((Message) message).getMessageNumber(), e); } return null; }
From source file:com.canoo.webtest.plugins.emailtest.EmailStoreHeader.java
/** * Calculate the result./*ww w .j av a2s .co m*/ * * @param message * @return The result */ protected String performOperation(final Message message) throws MessagingException { if (StringUtils.isEmpty(getPartIndex())) { return arrayToString(message.getHeader(getHeaderName())); } final Object content; try { content = message.getContent(); } catch (IOException e) { LOG.error("Error processing email message: ", e); throw new MessagingException("Error processing email message: " + e.getMessage()); } if (content instanceof Multipart) { final Multipart mp = (Multipart) content; final int part = ConversionUtil.convertToInt(getPartIndex(), 0); if (part >= mp.getCount()) { throw new StepFailedException("PartIndex too large.", this); } return arrayToString(mp.getBodyPart(part).getHeader(getHeaderName())); } throw new StepFailedException("PartIndex supplied for a non-MultiPart message.", this); }
From source file:com.glaf.mail.MxMailHelper.java
public void processMultipart(Multipart mp, Mail mail) throws Exception { for (int i = 0; i < mp.getCount(); i++) { String filename = (mp.getBodyPart(i)).getFileName(); if (filename == null) { this.processPart(mp.getBodyPart(i), mail); }/*from w ww. ja v a 2 s. c o m*/ } }
From source file:fr.gouv.culture.vitam.eml.EmlExtract.java
private static final String handleMultipartRecur(Multipart mp, Element identification, String id, VitamArgument argument, ConfigLoader config) throws MessagingException, IOException { int count = mp.getCount(); String result = ""; for (int i = 0; i < count; i++) { BodyPart bp = mp.getBodyPart(i); Object content = bp.getContent(); if (content instanceof String) { String[] cte = bp.getHeader("Content-Transfer-Encoding"); String[] aresult = null; if (cte != null && cte.length > 0) { aresult = extractContentType(bp.getContentType(), cte[0]); } else { aresult = extractContentType(bp.getContentType(), null); }/*from w w w . j av a 2 s. c o m*/ Element emlroot = XmlDom.factory.createElement("body"); // <identity format="Internet Message Format" mime="message/rfc822" puid="fmt/278" extensions="eml"/> Element subidenti = XmlDom.factory.createElement("identification"); Element identity = XmlDom.factory.createElement("identity"); identity.addAttribute("format", "Internet Message Body Format"); identity.addAttribute("mime", aresult[0] != null ? aresult[0] : "unknown"); identity.addAttribute("extensions", aresult[3] != null ? aresult[3].substring(1) : "unknown"); if (aresult[1] != null) { identity.addAttribute("charset", aresult[1]); } identification.add(identity); emlroot.add(subidenti); identification.add(emlroot); //result += " " + saveBody((String) content.toString(), aresult, id, argument, config); result += " " + saveBody(bp.getInputStream(), aresult, id, argument, config); // ignore string } else if (content instanceof InputStream) { // handle input stream if (argument.extractKeyword) { result += " " + addSubIdentities(identification, bp, (InputStream) content, argument, config); } else { addSubIdentities(identification, bp, (InputStream) content, argument, config); } } else if (content instanceof Message) { Message message = (Message) content; if (argument.extractKeyword) { result += " " + handleMessageRecur(message, identification, id + "_" + i, argument, config); } else { handleMessageRecur(message, identification, id + "_" + i, argument, config); } } else if (content instanceof Multipart) { Multipart mp2 = (Multipart) content; if (argument.extractKeyword) { result += " " + handleMultipartRecur(mp2, identification, id + "_" + i, argument, config); } else { handleMultipartRecur(mp2, identification, id + "_" + i, argument, config); } } } return result; }
From source file:eu.openanalytics.rsb.EmailDepositITCase.java
private BodyPart getMailBodyPart(final Multipart parts, final String contentType) throws MessagingException { for (int i = 0; i < parts.getCount(); i++) { final BodyPart part = parts.getBodyPart(i); if (StringUtils.startsWith(part.getContentType(), contentType)) { return part; }/*from www . jav a 2 s. c om*/ } throw new IllegalStateException("No part of type " + contentType + " found"); }
From source file:fr.gouv.culture.vitam.eml.EmlExtract.java
private static final String handleMultipart(Multipart mp, Element identification, String id, VitamArgument argument, ConfigLoader config) throws MessagingException, IOException { int count = mp.getCount(); String result = ""; identification.addAttribute(EMAIL_FIELDS.attNumber.name, Integer.toString(count - 1)); for (int i = 0; i < count; i++) { BodyPart bp = mp.getBodyPart(i); Object content = bp.getContent(); if (content instanceof String) { String[] cte = bp.getHeader("Content-Transfer-Encoding"); String[] aresult = null; if (cte != null && cte.length > 0) { aresult = extractContentType(bp.getContentType(), cte[0]); } else { aresult = extractContentType(bp.getContentType(), null); }//from ww w. j a v a 2 s . c om Element emlroot = XmlDom.factory.createElement("body"); // <identity format="Internet Message Format" mime="message/rfc822" puid="fmt/278" extensions="eml"/> Element subidenti = XmlDom.factory.createElement("identification"); Element identity = XmlDom.factory.createElement("identity"); identity.addAttribute("format", "Internet Message Body Format"); identity.addAttribute("mime", aresult[0] != null ? aresult[0] : "unknown"); identity.addAttribute("extensions", aresult[3] != null ? aresult[3].substring(1) : "unknown"); if (aresult[1] != null) { identity.addAttribute("charset", aresult[1]); } identification.add(identity); emlroot.add(subidenti); identification.add(emlroot); //result += " " + saveBody((String) content.toString(), aresult, id, argument, config); result += " " + saveBody(bp.getInputStream(), aresult, id, argument, config); } else if (content instanceof InputStream) { // handle input stream if (argument.extractKeyword) { result += " " + addSubIdentities(identification, bp, (InputStream) content, argument, config); } else { addSubIdentities(identification, bp, (InputStream) content, argument, config); } ((InputStream) content).close(); } else if (content instanceof Message) { Message message = (Message) content; // XXX perhaps using Commands.addFormatIdentification Element emlroot = XmlDom.factory.createElement(EMAIL_FIELDS.formatEML.name); // <identity format="Internet Message Format" mime="message/rfc822" puid="fmt/278" extensions="eml"/> Element subidenti = XmlDom.factory.createElement("identification"); Element identity = XmlDom.factory.createElement("identity"); identity.addAttribute("format", "Internet Message Format"); identity.addAttribute("mime", "message/rfc822"); identity.addAttribute("puid", "fmt/278"); identity.addAttribute("extensions", "eml"); identification.add(identity); emlroot.add(subidenti); identification.add(emlroot); if (argument.extractKeyword) { result += " " + extractInfoMessage((MimeMessage) message, emlroot, argument, config); } else { extractInfoMessage((MimeMessage) message, emlroot, argument, config); } } else if (content instanceof Multipart) { Multipart mp2 = (Multipart) content; if (argument.extractKeyword) { result += " " + handleMultipartRecur(mp2, identification, id + "_" + i, argument, config); } else { handleMultipartRecur(mp2, identification, id + "_" + i, argument, config); } } } return result; }
From source file:ch.algotrader.util.mail.EmailTransformer.java
/** * Parses any {@link Multipart} instances that contains attachments * * Will create the respective {@link EmailFragment}s representing those attachments. * @throws MessagingException/*from ww w .ja va 2 s . c o m*/ */ public void handleMultipart(Multipart multipart, List<EmailFragment> emailFragments) throws MessagingException { final int count = multipart.getCount(); for (int i = 0; i < count; i++) { BodyPart bodyPart = multipart.getBodyPart(i); String filename = bodyPart.getFileName(); String disposition = bodyPart.getDisposition(); if (filename == null && bodyPart instanceof MimeBodyPart) { filename = ((MimeBodyPart) bodyPart).getContentID(); } if (disposition == null) { //ignore message body } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE)) { try { try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); BufferedInputStream bis = new BufferedInputStream(bodyPart.getInputStream())) { IOUtils.copy(bis, bos); emailFragments.add(new EmailFragment(filename, bos.toByteArray())); if (LOGGER.isInfoEnabled()) { LOGGER.info(String.format("processing file: %s", new Object[] { filename })); } } } catch (IOException e) { throw new MessagingException("error processing streams", e); } } else { throw new MessagingException("unkown disposition " + disposition); } } }
From source file:com.canoo.webtest.plugins.emailtest.EmailMessageContentFilter.java
private void extractMultiPartMessage(final Multipart parts, final int partIndex) throws MessagingException { try {//w w w .j a v a2 s . co m if (partIndex >= parts.getCount()) { throw new StepFailedException("PartIndex too large.", this); } final BodyPart part = parts.getBodyPart(partIndex); final String contentType = part.getContentType(); if (!StringUtils.isEmpty(getContentType()) && !contentType.equals(getContentType())) { throw new MessagingException("Actual contentType of '" + contentType + "' did not match expected contentType of '" + fContentType + "'"); } final String disp = part.getDisposition(); final String filename = part.getFileName(); final InputStream inputStream = part.getInputStream(); if (Part.ATTACHMENT.equals(disp)) { fFilename = filename; } else { fFilename = getClass().getName(); } ContextHelper.defineAsCurrentResponse(getContext(), IOUtils.toString(inputStream), contentType, "http://" + fFilename); } catch (IOException e) { throw new MessagingException("Error extracting part: " + e.getMessage()); } }