List of usage examples for javax.mail Part getContentType
public String getContentType() throws MessagingException;
From source file:com.canoo.webtest.plugins.emailtest.EmailMessageStructureFilter.java
private static String processMessagePart(final Part part) throws MessagingException { final String disp = part.getDisposition(); final String contentType = part.getContentType(); if (Part.ATTACHMENT.equals(disp)) { return " <part type=\"attachment\" filename=\"" + part.getFileName() + "\" contentType=\"" + extractBaseContentType(contentType) + "\"/>" + LS; }/*w w w . j av a 2 s.co m*/ return " <part type=\"inline\" contentType=\"" + extractBaseContentType(contentType) + "\"/>" + LS; }
From source file:org.nuxeo.ecm.core.convert.plugins.text.extractors.RFC822ToTextConverter.java
protected static byte[] extractTextFromMessagePart(Part p) throws Exception { ContentType contentType = new ContentType(p.getContentType()); String baseType = contentType.getBaseType(); if (TXT_MT.equals(baseType)) { Object content = p.getContent(); if (content instanceof String) { return ((String) content).getBytes(); } else {/*from w ww . j a va 2 s. c om*/ return null; } } ConversionService cs = Framework.getLocalService(ConversionService.class); String converterName = cs.getConverterName(baseType, TXT_MT); if (converterName == null) { return null; } else { BlobHolder result = cs.convert(converterName, new SimpleBlobHolder(new FileBlob(p.getInputStream())), null); return result.getBlob().getByteArray(); } }
From source file:org.apache.hupa.server.utils.TestUtils.java
public static ArrayList<String> summaryzeContent(Object content, String contentType, final int spaces) throws IOException, MessagingException { ContenTypeArrayList ret = new ContenTypeArrayList(); ret.add(contentType, spaces);// w w w. jav a 2 s. c om if (content instanceof Multipart) { Multipart mp = (Multipart) content; contentType = mp.getContentType(); for (int i = 0; i < mp.getCount(); i++) { Part part = mp.getBodyPart(i); contentType = part.getContentType(); if (contentType.startsWith("text")) { ret.add(contentType, spaces + 1); } else if (contentType.startsWith("message/rfc822")) { MimeMessage msg = (MimeMessage) part.getDataHandler().getContent(); ret.addAll(summaryzeContent(msg.getContent(), msg.getContentType(), spaces + 1)); } else { if (part.getFileName() != null) { ret.add(part.getContentType(), part.getFileName(), spaces + 1); } else { ret.addAll(summaryzeContent(part.getContent(), contentType, spaces + 1)); } } } } return ret; }
From source file:org.silverpeas.util.mail.EMLExtractor.java
private static String getFileName(Part part) throws MessagingException { String fileName = part.getFileName(); if (!StringUtil.isDefined(fileName)) { try {/*w w w.j a v a2 s .c om*/ ContentType type = new ContentType(part.getContentType()); fileName = type.getParameter("name"); } catch (ParseException e) { SilverTrace.error("util", EMLExtractor.class.getSimpleName() + ".getFileName", "root.EX_NO_MESSAGE", e); } } if (StringUtil.isDefined(fileName) && fileName.startsWith("=?") && fileName.endsWith("?=")) { try { fileName = MimeUtility.decodeText(part.getFileName()); } catch (UnsupportedEncodingException e) { SilverTrace.error("util", EMLExtractor.class.getSimpleName() + ".getFileName", "root.EX_NO_MESSAGE", e); } } return fileName; }
From source file:org.apache.hupa.server.utils.MessageUtils.java
private static boolean handleMultiPartAlternative(Multipart mp, StringBuffer sbPlain) throws MessagingException, IOException { String text = null;/*from w w w .jav a2 s. c om*/ boolean isHTML = false; for (int i = 0; i < mp.getCount(); i++) { Part part = mp.getBodyPart(i); String contentType = part.getContentType().toLowerCase(); // we prefer html if (text == null && contentType.startsWith("text/plain")) { isHTML = false; text = (String) part.getContent(); } else if (contentType.startsWith("text/html")) { isHTML = true; text = (String) part.getContent(); } } sbPlain.append(text); return isHTML; }
From source file:dtw.webmail.model.JwmaMessagePartImpl.java
/** * Creates a <tt>JwmaMessagePartImpl</tt> instance from a given * <tt>javax.mail.Part</tt> instance. * * @param part a <tt>javax.mail.Part</tt> instance. * @param number the number of the part as <tt>int</tt>. * * @return the newly created instance./* ww w. j a v a2s . c o m*/ * @throws JwmaException if it fails to create the new instance. */ public static JwmaMessagePartImpl createJwmaMessagePartImpl(Part part, int number) throws JwmaException { JwmaMessagePartImpl partinfo = new JwmaMessagePartImpl(part, number); //content type try { partinfo.setContentType(part.getContentType()); //size int size = part.getSize(); //JwmaKernel.getReference().debugLog().write("Part size="+size); String fileName = part.getFileName(); //correct size of encoded parts String[] encoding = part.getHeader("Content-Transfer-Encoding"); if (fileName != null && encoding != null && encoding.length > 0 && (encoding[0].equalsIgnoreCase("base64") || encoding[0].equalsIgnoreCase("uuencode"))) { if ((fileName.startsWith("=?GB2312?B?") && fileName.endsWith("?="))) { byte[] decoded = Base64.decodeBase64(fileName.substring(11, fileName.indexOf("?=")).getBytes()); fileName = new String(decoded, "GB2312"); /*fileName = CipherUtils.decrypt(fileName); System.out.println("fileName----"+fileName);*/ //fileName = getFromBASE64(fileName.substring(11,fileName.indexOf("?="))); } else if ((fileName.startsWith("=?UTF-8?") || fileName.startsWith("=?UTF8?")) && fileName.endsWith("?=")) { fileName = MimeUtility.decodeText(fileName); } //an encoded file is about 35% smaller in reality, //so correct the size size = (int) (size * 0.65); } partinfo.setSize(size); //description partinfo.setDescription(part.getDescription()); //filename partinfo.setName(fileName); //textcontent if (partinfo.isMimeType("text/*")) { Object content = part.getContent(); if (content instanceof String) { partinfo.setTextContent((String) content); } else if (content instanceof InputStream) { InputStream in = (InputStream) content; ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] buffer = new byte[8192]; int amount = 0; while ((amount = in.read(buffer)) >= 0) { bout.write(buffer, 0, amount); } partinfo.setTextContent(new String(bout.toString())); } } } catch (Exception mex) { throw new JwmaException("jwma.messagepart.failedcreation", true).setException(mex); } return partinfo; }
From source file:org.apache.hupa.server.utils.MessageUtils.java
/** * Handle the parts of the given message. The method will call itself * recursively to handle all nested parts * * @param message the MimeMessage/*from www. ja va2s . co m*/ * @param content the current processing Content * @param sbPlain the StringBuffer to fill with text * @param attachmentList ArrayList with attachments * @throws UnsupportedEncodingException * @throws MessagingException * @throws IOException */ public static boolean handleParts(Message message, Object content, StringBuffer sbPlain, ArrayList<MessageAttachment> attachmentList) throws UnsupportedEncodingException, MessagingException, IOException { boolean isHTML = false; if (content instanceof String) { if (message.getContentType().toLowerCase().startsWith("text/html")) { isHTML = true; } else { isHTML = false; } sbPlain.append((String) content); } else if (content instanceof Multipart) { Multipart mp = (Multipart) content; 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 MessageAttachmentImpl(); 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:com.cubusmail.mail.text.MessageTextUtil.java
/** * Reads the string out of part's input stream. On first try the input * stream retrieved by <code>javax.mail.Part.getInputStream()</code> is * used. If an I/O error occurs (<code>java.io.IOException</code>) then the * next try is with part's raw input stream. If everything fails an empty * string is returned.//from w w w .j a v a 2 s .c om * * @param p * - the <code>javax.mail.Part</code> object * @param ct * - the part's content type * @return the string read from part's input stream or the empty string "" * if everything failed * @throws MessagingException * - if an error occurs in part's getter methods */ public static String readPart(final Part p) throws MessagingException { String contentType = p.getContentType(); ContentType type = new ContentType(contentType); /* * Use specified charset if available else use default one */ String charset = type.getParameter("charset"); if (null == charset || charset.equalsIgnoreCase(CubusConstants.US_ASCII)) { charset = CubusConstants.DEFAULT_CHARSET; } try { return readStream(p.getInputStream(), charset); } catch (final IOException e) { /* * Try to get data from raw input stream */ final InputStream inStream; if (p instanceof MimeBodyPart) { final MimeBodyPart mpb = (MimeBodyPart) p; inStream = mpb.getRawInputStream(); } else if (p instanceof MimeMessage) { final MimeMessage mm = (MimeMessage) p; inStream = mm.getRawInputStream(); } else { inStream = null; } if (inStream == null) { /* * Neither a MimeBodyPart nor a MimeMessage */ return ""; } try { return readStream(inStream, charset); } catch (final IOException e1) { log.error(e1.getLocalizedMessage(), e1); return e1.getLocalizedMessage(); // return STR_EMPTY; } finally { try { inStream.close(); } catch (final IOException e1) { log.error(e1.getLocalizedMessage(), e1); } } } }
From source file:mitm.common.mail.BodyPartUtils.java
/** * Extracts the message from the RFC822 attachment. * @throws MessagingException // w w w . j a v a 2s.c om * @throws IOException */ public static MimeMessage extractFromRFC822(Part rfc822) throws IOException, MessagingException { if (!rfc822.isMimeType("message/rfc822")) { throw new MessagingException("Part is-not-a message/rfc822 but " + rfc822.getContentType()); } return new MimeMessage(MailSession.getDefaultSession(), rfc822.getInputStream()); }
From source file:org.elasticsearch.river.email.EmailToJson.java
public static void getMailTextContent(Part part, StringBuffer content) throws MessagingException, IOException { boolean isContainTextAttach = part.getContentType().indexOf("name") > 0; if (part.isMimeType("text/*") && !isContainTextAttach) { content.append(part.getContent().toString()); } else if (part.isMimeType("message/rfc822")) { getMailTextContent((Part) part.getContent(), content); } else if (part.isMimeType("multipart/*")) { Multipart multipart = (Multipart) part.getContent(); int partCount = multipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart bodyPart = multipart.getBodyPart(i); getMailTextContent(bodyPart, content); }// w w w . ja v a 2 s. c o m } }