List of usage examples for javax.mail Part getContentType
public String getContentType() throws MessagingException;
From source file:com.cubusmail.server.services.RetrieveImageServlet.java
@Override public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {//from ww w. j a v a 2s. c o m String messageId = request.getParameter("messageId"); String attachmentIndex = request.getParameter("attachmentIndex"); boolean isThumbnail = Boolean.valueOf(request.getParameter("thumbnail")).booleanValue(); if (messageId != null) { IMailbox mailbox = SessionManager.get().getMailbox(); Message msg = mailbox.getCurrentFolder().getMessageById(Long.parseLong(messageId)); if (isThumbnail) { List<MimePart> attachmentList = MessageUtils.attachmentsFromPart(msg); int index = Integer.valueOf(attachmentIndex); MimePart retrievePart = attachmentList.get(index); ContentType contentType = new ContentType(retrievePart.getContentType()); response.setContentType(contentType.getBaseType()); BufferedInputStream bufInputStream = new BufferedInputStream(retrievePart.getInputStream()); OutputStream outputStream = response.getOutputStream(); writeScaledImage(bufInputStream, outputStream); bufInputStream.close(); outputStream.flush(); outputStream.close(); } else { Part imagePart = findImagePart(msg); if (imagePart != null) { ContentType contentType = new ContentType(imagePart.getContentType()); response.setContentType(contentType.getBaseType()); BufferedInputStream bufInputStream = new BufferedInputStream(imagePart.getInputStream()); OutputStream outputStream = response.getOutputStream(); byte[] inBuf = new byte[1024]; int len = 0; int total = 0; while ((len = bufInputStream.read(inBuf)) > 0) { outputStream.write(inBuf, 0, len); total += len; } bufInputStream.close(); outputStream.flush(); outputStream.close(); } } } } catch (Exception ex) { log.error(ex.getMessage(), ex); } }
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 //from w ww. ja v a2 s .c om * @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:org.silverpeas.core.mail.extractor.EMLExtractor.java
private String getFileName(Part part) throws MessagingException { String fileName = part.getFileName(); if (!StringUtil.isDefined(fileName)) { try {//from w w w . java 2 s. c o m ContentType type = new ContentType(part.getContentType()); fileName = type.getParameter("name"); } catch (ParseException e) { SilverLogger.getLogger(this).error(e); } } if (StringUtil.isDefined(fileName) && fileName.startsWith("=?") && fileName.endsWith("?=")) { try { fileName = MimeUtility.decodeText(part.getFileName()); } catch (UnsupportedEncodingException e) { SilverLogger.getLogger(this).error(e); } } return fileName; }
From source file:org.nuxeo.cm.mail.actionpipe.ExtractMessageInformation.java
@SuppressWarnings("unchecked") protected static void getAttachmentParts(Part p, String defaultFilename, MimetypeRegistry mimeService, ExecutionContext context) throws MessagingException, IOException { String filename = getFilename(p, defaultFilename); List<Blob> blobs = (List<Blob>) context.get(ATTACHMENTS_KEY); if (!p.isMimeType("multipart/*")) { String disp = p.getDisposition(); // no disposition => consider it may be body if (disp == null && !context.containsKey(BODY_KEY)) { // this will need to be parsed context.put(BODY_KEY, p.getContent()); } else if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || (disp.equalsIgnoreCase(Part.INLINE) && !(p.isMimeType("text/*") || p.isMimeType("image/*"))))) { log.debug("Saving attachment to file " + filename); Blob blob = Blobs.createBlob(p.getInputStream()); String mime = DEFAULT_BINARY_MIMETYPE; try { if (mimeService != null) { ContentType contentType = new ContentType(p.getContentType()); mime = mimeService.getMimetypeFromFilenameAndBlobWithDefault(filename, blob, contentType.getBaseType()); }/*from w w w.ja v a 2 s . co m*/ } catch (MimetypeDetectionException e) { log.error(e); } blob.setMimeType(mime); blob.setFilename(filename); blobs.add(blob); // for debug // File f = new File(filename); // ((MimeBodyPart) p).saveFile(f); log.debug("---------------------------"); } else { log.debug(String.format("Ignoring part with type %s", p.getContentType())); } } if (p.isMimeType("multipart/*")) { log.debug("This is a Multipart"); log.debug("---------------------------"); Multipart mp = (Multipart) p.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) { getAttachmentParts(mp.getBodyPart(i), defaultFilename, mimeService, context); } } else if (p.isMimeType(MESSAGE_RFC822_MIMETYPE)) { log.debug("This is a Nested Message"); log.debug("---------------------------"); getAttachmentParts((Part) p.getContent(), defaultFilename, mimeService, context); } }
From source file:de.contentreich.alfresco.repo.email.EMLTransformer.java
private void processPreviewMultiPart(Multipart multipart, Map<String, String> parts) throws MessagingException, IOException { logger.debug("Processing multipart of type {}", multipart.getContentType()); // FIXME : Implement strict Depth or breadth first ? for (int i = 0, n = multipart.getCount(); i < n; i++) { Part part = multipart.getBodyPart(i); logger.debug("Processing part name {}, disposition = {}, type type = {}", new Object[] { part.getFileName(), part.getDisposition(), part.getContentType() }); if (part.getContent() instanceof Multipart) { processPreviewMultiPart((Multipart) part.getContent(), parts); } else if (part.getContentType().contains("text")) { String key = part.getContentType().split(";")[0]; String content = null; logger.debug("Add part with content type {} using key {}", part.getContentType(), key); if (key.endsWith("html")) { // <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> // Breaks preview ! content = part.getContent().toString().replaceAll("(?i)(?s)<meta.*charset=[^>]*>", ""); } else { content = part.getContent().toString(); }// w w w .j ava 2 s. co m appendPreviewContent(parts, key, content); } } }
From source file:org.silverpeas.core.mail.extractor.EMLExtractor.java
private boolean isTextPart(Part part) throws MessagingException { String disposition = part.getDisposition(); if (!Part.ATTACHMENT.equals(disposition) && !Part.INLINE.equals(disposition)) { try {//from w w w .j a v a 2 s. co m ContentType type = new ContentType(part.getContentType()); return "text".equalsIgnoreCase(type.getPrimaryType()); } catch (ParseException e) { SilverLogger.getLogger(this).error(e); } } else if (Part.INLINE.equals(disposition)) { try { ContentType type = new ContentType(part.getContentType()); return "text".equalsIgnoreCase(type.getPrimaryType()) && getFileName(part) == null; } catch (ParseException e) { SilverLogger.getLogger(this).error(e); } } return false; }
From source file:com.szmslab.quickjavamail.receive.MessageLoader.java
/** * ?????MessageContent????/*from w w w .j a v a 2 s. c o m*/ * * @param multiPart * ? * @param msgContent * ???? * @throws MessagingException * @throws IOException */ private void setMultipartContent(Multipart multiPart, MessageContent msgContent) throws MessagingException, IOException { for (int i = 0; i < multiPart.getCount(); i++) { Part part = multiPart.getBodyPart(i); if (part.getContentType().indexOf("multipart") >= 0) { setMultipartContent((Multipart) part.getContent(), msgContent); } else { String disposition = part.getDisposition(); if (Part.ATTACHMENT.equals(disposition)) { // Disposition?"attachment"???ContentType???? msgContent.attachmentFileList.add(new AttachmentFile(MimeUtility.decodeText(part.getFileName()), part.getDataHandler().getDataSource())); } else { if (part.isMimeType("text/html")) { msgContent.html = part.getContent().toString(); } else if (part.isMimeType("text/plain")) { msgContent.text = part.getContent().toString(); } else { // Disposition?"inline"???ContentType?? if (Part.INLINE.equals(disposition)) { String cid = ""; if (part instanceof MimeBodyPart) { MimeBodyPart mimePart = (MimeBodyPart) part; cid = mimePart.getContentID(); } msgContent.inlineImageFileList .add(new InlineImageFile(cid, MimeUtility.decodeText(part.getFileName()), part.getDataHandler().getDataSource())); } } } } } }
From source file:org.silverpeas.components.mailinglist.service.job.MailProcessor.java
protected String getFileName(Part part) throws MessagingException { String fileName = part.getFileName(); if (fileName == null) { try {/*w w w. ja v a2 s .c om*/ ContentType type = new ContentType(part.getContentType()); fileName = type.getParameter("name"); } catch (ParseException e) { logger.error(e.getMessage(), e); } } return fileName; }
From source file:org.silverpeas.util.mail.EMLExtractor.java
private boolean isTextPart(Part part) throws MessagingException { String disposition = part.getDisposition(); if (!Part.ATTACHMENT.equals(disposition) && !Part.INLINE.equals(disposition)) { try {//w w w . j a v a 2s. co m ContentType type = new ContentType(part.getContentType()); return "text".equalsIgnoreCase(type.getPrimaryType()); } catch (ParseException e) { SilverTrace.error("util", EMLExtractor.class.getSimpleName() + ".getFileName", "root.EX_NO_MESSAGE", e); } } else if (Part.INLINE.equals(disposition)) { try { ContentType type = new ContentType(part.getContentType()); return "text".equalsIgnoreCase(type.getPrimaryType()) && getFileName(part) == null; } catch (ParseException e) { SilverTrace.error("util", EMLExtractor.class.getSimpleName() + ".getFileName", "root.EX_NO_MESSAGE", e); } } return false; }
From source file:com.silverpeas.mailinglist.service.job.MailProcessor.java
protected String getFileName(Part part) throws MessagingException { String fileName = part.getFileName(); if (fileName == null) { try {/* ww w . j a v a2s.co m*/ ContentType type = new ContentType(part.getContentType()); fileName = type.getParameter("name"); } catch (ParseException e) { e.printStackTrace(); } } return fileName; }