List of usage examples for javax.mail Part getDataHandler
public DataHandler getDataHandler() throws MessagingException;
From source file:immf.Util.java
public static void setFileName(Part part, String filename, String charset, String lang) throws MessagingException { ContentDisposition disposition;/*from w w w.j a v a 2 s . c om*/ String[] strings = part.getHeader("Content-Disposition"); if (strings == null || strings.length < 1) { disposition = new ContentDisposition(Part.ATTACHMENT); } else { disposition = new ContentDisposition(strings[0]); disposition.getParameterList().remove("filename"); } part.setHeader("Content-Disposition", disposition.toString() + encodeParameter("filename", filename, charset, lang)); ContentType cType; strings = part.getHeader("Content-Type"); if (strings == null || strings.length < 1) { cType = new ContentType(part.getDataHandler().getContentType()); } else { cType = new ContentType(strings[0]); } try { // I want to public the MimeUtility#doEncode()!!! String mimeString = MimeUtility.encodeWord(filename, charset, "B"); // cut <CRLF>... StringBuffer sb = new StringBuffer(); int i; while ((i = mimeString.indexOf('\r')) != -1) { sb.append(mimeString.substring(0, i)); mimeString = mimeString.substring(i + 2); } sb.append(mimeString); cType.setParameter("name", new String(sb)); } catch (UnsupportedEncodingException e) { throw new MessagingException("Encoding error", e); } part.setHeader("Content-Type", cType.toString()); }
From source file:com.szmslab.quickjavamail.receive.MessageLoader.java
/** * ?????MessageContent????/*from w ww . j ava 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:it.greenvulcano.gvesb.virtual.http.HTTPCallOperation.java
private void dumpPart(Part p, Element msg, Document doc) throws Exception { Element content = null;/*from w w w .j a v a2 s . c o m*/ if (p.isMimeType("text/plain")) { content = doc.createElement("PlainMessage"); Text body = doc.createTextNode((String) p.getContent()); content.appendChild(body); } else if (p.isMimeType("text/html")) { content = doc.createElement("HTMLMessage"); CDATASection body = doc.createCDATASection((String) p.getContent()); content.appendChild(body); } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); int count = mp.getCount(); content = doc.createElement("Multipart"); for (int i = 0; i < count; i++) { dumpPart(mp.getBodyPart(i), content, doc); } } else if (p.isMimeType("message/rfc822")) { content = doc.createElement("NestedMessage"); dumpPart((Part) p.getContent(), content, doc); } else { content = doc.createElement("EncodedContent"); DataHandler dh = p.getDataHandler(); OutputStream os = new ByteArrayOutputStream(); Base64EncodingOutputStream b64os = new Base64EncodingOutputStream(os); dh.writeTo(b64os); b64os.flush(); b64os.close(); content.appendChild(doc.createTextNode(os.toString())); } msg.appendChild(content); String filename = p.getFileName(); if (filename != null) { content.setAttribute("file-name", filename); } String ct = p.getContentType(); if (ct != null) { content.setAttribute("content-type", ct); } String desc = p.getDescription(); if (desc != null) { content.setAttribute("description", desc); } }
From source file:edu.stanford.muse.email.EmailFetcherStats.java
private List<String> getAttachmentNames(MimeMessage m, Part p) throws MessagingException, IOException { List<String> result = new ArrayList<String>(); try {//from w ww .j a va 2s.c o m if (p.isMimeType("multipart/*") || p.isMimeType("message/rfc822")) { if (p.isMimeType("multipart/alternative")) return result; // ignore alternative's because real attachments don't have alternatives DataHandler dh = p.getDataHandler(); DataSource ds = dh.getDataSource(); if (ds instanceof MultipartDataSource) { MultipartDataSource mpds = (MultipartDataSource) ds; for (int i = 0; i < mpds.getCount(); i++) result.addAll(getAttachmentNames(m, mpds.getBodyPart(i))); } else { String name = ds.getName(); if (!Util.nullOrEmpty(name)) result.add(name); } } else { String filename = p.getFileName(); if (filename != null) result.add(filename); } } catch (Exception e) { // sometimes we see javax.mail.MessagingException: Unable to load BODYSTRUCTURE // in this case, just ignore, not much we can do i guess. Util.print_exception(e, log); } return result; }
From source file:org.apache.axiom.attachments.Attachments.java
/** * @return the Next valid MIME part + store the Part in the Parts List * @throws OMException throw if content id is null or if two MIME parts contain the same * content-ID & the exceptions throws by getPart() *//* w w w . j a va2s . c o m*/ private DataHandler getNextPartDataHandler() throws OMException { if (endOfStreamReached) { return null; } Part nextPart; nextPart = getPart(); if (nextPart == null) { return null; } else try { long size = nextPart.getSize(); String partContentID; DataHandler dataHandler; try { partContentID = nextPart.getContentID(); if (partContentID == null & partIndex == 1) { String id = "firstPart_" + UIDGenerator.generateContentId(); firstPartId = id; if (size > 0) { dataHandler = nextPart.getDataHandler(); } else { // Either the mime part is empty or the stream ended without having // a MIME message terminator dataHandler = new DataHandler(new ByteArrayDataSource(new byte[] {})); } addDataHandler(id, dataHandler); return dataHandler; } if (partContentID == null) { throw new OMException("Part content ID cannot be blank for non root MIME parts"); } if ((partContentID.indexOf("<") > -1) & (partContentID.indexOf(">") > -1)) { partContentID = partContentID.substring(1, (partContentID.length() - 1)); } if (partIndex == 1) { firstPartId = partContentID; } if (attachmentsMap.containsKey(partContentID)) { throw new OMException("Two MIME parts with the same Content-ID not allowed."); } if (size > 0) { dataHandler = nextPart.getDataHandler(); } else { // Either the mime part is empty or the stream ended without having // a MIME message terminator dataHandler = new DataHandler(new ByteArrayDataSource(new byte[] {})); } addDataHandler(partContentID, dataHandler); return dataHandler; } catch (MessagingException e) { throw new OMException("Error reading Content-ID from the Part." + e); } } catch (MessagingException e) { throw new OMException(e); } }
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); LOG.trace("Part #" + i + ": " + part); if (part.isMimeType("multipart/*")) { LOG.trace("Part #" + i + ": is mimetype: multipart/*"); extractAttachmentsFromMultipart((Multipart) part.getContent(), map); } else {//from w w w . j a va2 s. co m 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.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 w w.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.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 w w w .j a v a 2 s . com * @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: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);/*from ww w. j a v a 2s . c o m*/ 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.apache.james.transport.mailets.SMIMEDecrypt.java
private String text(Part mimePart) throws IOException, MessagingException { return IOUtils.toString(mimePart.getDataHandler().getInputStream()); }