Java Code Examples for javax.mail.Part#getInputStream()
The following examples show how to use
javax.mail.Part#getInputStream() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MimeMessageParser.java From eml-to-pdf-converter with Apache License 2.0 | 6 votes |
/** * Get the String Content of a MimePart. * @param p MimePart * @return Content as String * @throws IOException * @throws MessagingException */ private static String getStringContent(Part p) throws IOException, MessagingException { Object content = null; try { content = p.getContent(); } catch (Exception e) { Logger.debug("Email body could not be read automatically (%s), we try to read it anyway.", e.toString()); // most likely the specified charset could not be found content = p.getInputStream(); } String stringContent = null; if (content instanceof String) { stringContent = (String) content; } else if (content instanceof InputStream) { stringContent = new String(ByteStreams.toByteArray((InputStream) content), "utf-8"); } return stringContent; }
Example 2
Source File: AttachmentFileNameIs.java From james-project with Apache License 2.0 | 6 votes |
/** * Checks if <I>part</I> is a zip containing a file that matches with at least one of the <CODE>masks</CODE>. * *@param part */ protected boolean matchFoundInZip(Part part) throws MessagingException, IOException { try (ZipInputStream zis = new ZipInputStream(part.getInputStream())) { while (true) { ZipEntry zipEntry = zis.getNextEntry(); if (zipEntry == null) { break; } String fileName = zipEntry.getName(); if (matchFound(fileName)) { if (isDebug) { LOGGER.debug("matched {}({})", part.getFileName(), fileName); } return true; } } return false; } }
Example 3
Source File: SubethaEmailMessagePart.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
/** * Object can be built on existing message part only. * * @param messagePart Message part. */ public SubethaEmailMessagePart(Part messagePart) { ParameterCheck.mandatory("messagePart", messagePart); try { fileSize = messagePart.getSize(); fileName = messagePart.getFileName(); contentType = messagePart.getContentType(); Matcher matcher = encodingExtractor.matcher(contentType); if (matcher.find()) { encoding = matcher.group(1); if (!Charset.isSupported(encoding)) { throw new EmailMessageException(ERR_UNSUPPORTED_ENCODING, encoding); } } try { contentInputStream = messagePart.getInputStream(); } catch (Exception ex) { throw new EmailMessageException(ERR_FAILED_TO_READ_CONTENT_STREAM, ex.getMessage()); } } catch (MessagingException e) { throw new EmailMessageException(ERR_INCORRECT_MESSAGE_PART, e.getMessage()); } }
Example 4
Source File: EmailReader.java From baleen with Apache License 2.0 | 5 votes |
private void writeFileToDisk(File destFile, Part part) throws IOException, MessagingException { try (FileOutputStream output = new FileOutputStream(destFile); InputStream input = part.getInputStream()) { byte[] buffer = new byte[4096]; int byteRead; while ((byteRead = input.read(buffer)) != -1) { output.write(buffer, 0, byteRead); } } catch (IOException ex) { throw new IOException("Unable to save attachment", ex); } }
Example 5
Source File: MessageParser.java From translationstudio8 with GNU General Public License v2.0 | 5 votes |
/** * 获取消息附件中的文件. * @return List<ResourceFileBean> * 文件的集合(每个 ResourceFileBean 对象中存放文件名和 InputStream 对象) * @throws MessagingException * the messaging exception * @throws IOException * Signals that an I/O exception has occurred. */ public List<ResourceFileBean> getFiles() throws MessagingException, IOException { List<ResourceFileBean> resourceList = new ArrayList<ResourceFileBean>(); Object content = message.getContent(); Multipart mp = null; if (content instanceof Multipart) { mp = (Multipart) content; } else { return resourceList; } for (int i = 0, n = mp.getCount(); i < n; i++) { Part part = mp.getBodyPart(i); //此方法返回 Part 对象的部署类型。 String disposition = part.getDisposition(); //Part.ATTACHMENT 指示 Part 对象表示附件。 //Part.INLINE 指示 Part 对象以内联方式显示。 if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) { //part.getFileName():返回 Part 对象的文件名。 String fileName = MimeUtility.decodeText(part.getFileName()); //此方法为 Part 对象返回一个 InputStream 对象 InputStream is = part.getInputStream(); resourceList.add(new ResourceFileBean(fileName, is)); } else if (disposition == null) { //附件也可以没有部署类型的方式存在 getRelatedPart(part, resourceList); } } return resourceList; }
Example 6
Source File: MessageParser.java From translationstudio8 with GNU General Public License v2.0 | 5 votes |
/** * 获取消息附件中的文件. * @param part * Part 对象 * @param resourceList * 文件的集合(每个 ResourceFileBean 对象中存放文件名和 InputStream 对象) * @throws MessagingException * the messaging exception * @throws IOException * Signals that an I/O exception has occurred. */ private void getRelatedPart(Part part, List<ResourceFileBean> resourceList) throws MessagingException, IOException { //验证 Part 对象的 MIME 类型是否与指定的类型匹配。 if (part.isMimeType("multipart/related")) { Multipart mulContent = (Multipart) part.getContent(); for (int j = 0, m = mulContent.getCount(); j < m; j++) { Part contentPart = mulContent.getBodyPart(j); if (!contentPart.isMimeType("text/*")) { String fileName = "Resource"; //此方法返回 Part 对象的内容类型。 String type = contentPart.getContentType(); if (type != null) { type = type.substring(0, type.indexOf("/")); } fileName = fileName + "[" + type + "]"; if (contentPart.getHeader("Content-Location") != null && contentPart.getHeader("Content-Location").length > 0) { fileName = contentPart.getHeader("Content-Location")[0]; } InputStream is = contentPart.getInputStream(); resourceList.add(new ResourceFileBean(fileName, is)); } } } else { Multipart mp = null; Object content = part.getContent(); if (content instanceof Multipart) { mp = (Multipart) content; } else { return; } for (int i = 0, n = mp.getCount(); i < n; i++) { Part body = mp.getBodyPart(i); getRelatedPart(body, resourceList); } } }
Example 7
Source File: MessageParser.java From tmxeditor8 with GNU General Public License v2.0 | 5 votes |
/** * 获取消息附件中的文件. * @return List<ResourceFileBean> * 文件的集合(每个 ResourceFileBean 对象中存放文件名和 InputStream 对象) * @throws MessagingException * the messaging exception * @throws IOException * Signals that an I/O exception has occurred. */ public List<ResourceFileBean> getFiles() throws MessagingException, IOException { List<ResourceFileBean> resourceList = new ArrayList<ResourceFileBean>(); Object content = message.getContent(); Multipart mp = null; if (content instanceof Multipart) { mp = (Multipart) content; } else { return resourceList; } for (int i = 0, n = mp.getCount(); i < n; i++) { Part part = mp.getBodyPart(i); //此方法返回 Part 对象的部署类型。 String disposition = part.getDisposition(); //Part.ATTACHMENT 指示 Part 对象表示附件。 //Part.INLINE 指示 Part 对象以内联方式显示。 if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) { //part.getFileName():返回 Part 对象的文件名。 String fileName = MimeUtility.decodeText(part.getFileName()); //此方法为 Part 对象返回一个 InputStream 对象 InputStream is = part.getInputStream(); resourceList.add(new ResourceFileBean(fileName, is)); } else if (disposition == null) { //附件也可以没有部署类型的方式存在 getRelatedPart(part, resourceList); } } return resourceList; }
Example 8
Source File: MessageParser.java From tmxeditor8 with GNU General Public License v2.0 | 5 votes |
/** * 获取消息附件中的文件. * @param part * Part 对象 * @param resourceList * 文件的集合(每个 ResourceFileBean 对象中存放文件名和 InputStream 对象) * @throws MessagingException * the messaging exception * @throws IOException * Signals that an I/O exception has occurred. */ private void getRelatedPart(Part part, List<ResourceFileBean> resourceList) throws MessagingException, IOException { //验证 Part 对象的 MIME 类型是否与指定的类型匹配。 if (part.isMimeType("multipart/related")) { Multipart mulContent = (Multipart) part.getContent(); for (int j = 0, m = mulContent.getCount(); j < m; j++) { Part contentPart = mulContent.getBodyPart(j); if (!contentPart.isMimeType("text/*")) { String fileName = "Resource"; //此方法返回 Part 对象的内容类型。 String type = contentPart.getContentType(); if (type != null) { type = type.substring(0, type.indexOf("/")); } fileName = fileName + "[" + type + "]"; if (contentPart.getHeader("Content-Location") != null && contentPart.getHeader("Content-Location").length > 0) { fileName = contentPart.getHeader("Content-Location")[0]; } InputStream is = contentPart.getInputStream(); resourceList.add(new ResourceFileBean(fileName, is)); } } } else { Multipart mp = null; Object content = part.getContent(); if (content instanceof Multipart) { mp = (Multipart) content; } else { return; } for (int i = 0, n = mp.getCount(); i < n; i++) { Part body = mp.getBodyPart(i); getRelatedPart(body, resourceList); } } }
Example 9
Source File: EmailUtils.java From ogham with Apache License 2.0 | 3 votes |
/** * Get the content as byte array of a particular part. * * <p> * If the part is null or the content stream is null, then * null is returned. * * @param part * the part * @return the content as byte array or null * @throws IOException * when part can't be read * @throws MessagingException * when message can't be read */ @SuppressWarnings("squid:S1168") // return null on purpose (to be able to distinguish empty content from no content at all in tests) public static byte[] getContent(Part part) throws IOException, MessagingException { if (part == null) { return null; } InputStream stream = part.getInputStream(); if (stream == null) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(stream, baos); return baos.toByteArray(); }