List of usage examples for javax.mail Multipart getCount
public synchronized int getCount() throws MessagingException
From source file:javamailclient.GmailAPI.java
/** * Return the primary text content of the message. *//*from ww w .java 2 s .c om*/ private static String getMessageBody(Part p) throws MessagingException, IOException { if (p.isMimeType("text/*")) { String s = (String) p.getContent(); return s; } if (p.isMimeType("multipart/alternative")) { // prefer html text over plain text Multipart mp = (Multipart) p.getContent(); String text = null; for (int i = 0; i < mp.getCount(); i++) { Part bp = mp.getBodyPart(i); if (bp.isMimeType("text/plain")) { if (text == null) { text = getMessageBody(bp); } continue; } else if (bp.isMimeType("text/html")) { String s = getMessageBody(bp); if (s != null) { return s; } } else { return getMessageBody(bp); } } return text; } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) { String s = getMessageBody(mp.getBodyPart(i)); if (s != null) { return s; } } } return null; }
From source file:org.elasticsearch.river.email.EmailToJson.java
public static List<AttachmentInfo> saveAttachmentToWeedFs(Part message, List<AttachmentInfo> attachments, EmailRiverConfig config)//from www.j a v a 2s . co m throws UnsupportedEncodingException, MessagingException, FileNotFoundException, IOException { if (attachments == null) { attachments = new ArrayList<AttachmentInfo>(); } boolean hasAttachment = false; try { hasAttachment = isContainAttachment(message); } catch (MessagingException e) { logger.error("save attachment", e); } catch (IOException e) { logger.error("save attachment", e); } if (hasAttachment) { if (message.isMimeType("multipart/*")) { Multipart multipart = (Multipart) message.getContent(); int partCount = multipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart bodyPart = multipart.getBodyPart(i); String disp = bodyPart.getDisposition(); if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) { InputStream is = bodyPart.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len = -1; while ((len = bis.read()) != -1) { bos.write(len); } bos.close(); bis.close(); byte[] data = bos.toByteArray(); String fileId = uploadFileToWeedfs(data, config); if (fileId != null) { AttachmentInfo info = new AttachmentInfo(); info.fileId = fileId; info.fileName = decodeText(bodyPart.getFileName()); info.fileSize = data.length; attachments.add(info); } } else if (bodyPart.isMimeType("multipart/*")) { attachments.addAll(saveAttachmentToWeedFs(bodyPart, attachments, config)); } else { String contentType = bodyPart.getContentType(); if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) { attachments.addAll(saveAttachmentToWeedFs(bodyPart, attachments, config)); } } } } else if (message.isMimeType("message/rfc822")) { attachments.addAll(saveAttachmentToWeedFs(message, attachments, config)); } } return attachments; }
From source file:mitm.common.mail.BodyPartUtils.java
/** * Searches for an embedded RFC822 messages. Returns the first embedded RFC822 it finds. * Only one level deep is searched.//w w w . j a v a 2 s . c o m * * Returns null if there are no embedded message. */ public static MimeMessage searchForRFC822(MimeMessage message) throws MessagingException, IOException { /* * Fast fail. Only multipart mixed messages are supported. */ if (!message.isMimeType("multipart/mixed")) { return null; } Multipart mp; try { mp = (Multipart) message.getContent(); } catch (IOException e) { throw new MessagingException("Error getting message content.", e); } MimeMessage embeddedMessage = null; for (int i = 0; i < mp.getCount(); i++) { BodyPart part = mp.getBodyPart(i); if (part.isMimeType("message/rfc822")) { embeddedMessage = BodyPartUtils.extractFromRFC822(part); break; } } return embeddedMessage; }
From source file:com.intranet.intr.inbox.EmpControllerInbox.java
private static correoNoLeidos analizaParteDeMensaje2(correoNoLeidos correo, List<String> arc, List<String> rar, Part unaParte) {/*from w w w .j a va 2s. c o m*/ String r = null; try { System.out.println("CORREO NUM: "); // Si es multipart, se analiza cada una de sus partes recursivamente. if (unaParte.isMimeType("multipart/*")) { Multipart multi; multi = (Multipart) unaParte.getContent(); System.out.println("for MULTIPART"); for (int j = 0; j < multi.getCount(); j++) { System.out.println("Si es multi"); analizaParteDeMensaje2(correo, arc, rar, multi.getBodyPart(j)); } } else { // Si es texto, se escribe el texto. if (unaParte.isMimeType("text/*")) { System.out.println("Texto " + unaParte.getContentType()); System.out.println(unaParte.getContent()); System.out.println("---------------------------------"); correo.setContenido((String) unaParte.getContent()); } // Si es imagen, se guarda en fichero y se visualiza en JFrame if (unaParte.isMimeType("image/*")) { System.out.println("IMAGEN SYSTEM"); System.out.println("Imagen " + unaParte.getContentType()); System.out.println("Fichero=" + unaParte.getFileName()); System.out.println("---------------------------------"); arc.add("/Intranet/resources/archivosMail/verCorreo/" + salvaImagenEnFichero(unaParte)); //visualizaImagenEnJFrame(unaParte); } else { System.out.println("ELSE img"); // Si no es ninguna de las anteriores, se escribe en pantalla // el tipo. System.out.println("Recibido " + unaParte.getContentType()); System.out.println("---------------------------------"); if (salvaImagenEnFichero(unaParte) != null & salvaImagenEnFichero(unaParte) != "") { rar.add(salvaImagenEnFichero(unaParte)); } correo.setContenido((String) unaParte.getContent()); } } } catch (Exception e) { e.printStackTrace(); } correo.setImagenes(arc); correo.setRar(rar); return correo; }
From source file:com.panet.imeta.job.entries.getpop.JobEntryGetPOP.java
public static void handleMultipart(String foldername, Multipart multipart) throws MessagingException, IOException { for (int i = 0, n = multipart.getCount(); i < n; i++) { handlePart(foldername, multipart.getBodyPart(i)); }/* ww w . ja v a2 s. c om*/ }
From source file:com.cubusmail.mail.util.MessageUtils.java
/** * @param part/*from w w w . j a v a2 s . c o m*/ * @return * @throws MessagingException * @throws IOException */ public static boolean hasAttachments(Part part) throws MessagingException, IOException { try { if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); for (int i = 0; i < mp.getCount(); i++) { MimeBodyPart bodyPart = (MimeBodyPart) mp.getBodyPart(i); if (Part.ATTACHMENT.equals(bodyPart.getDisposition()) || Part.INLINE.equals(bodyPart.getDisposition())) { return true; } } } } catch (Exception e) { log.error(e.getMessage(), e); } return false; }
From source file:com.cubusmail.mail.util.MessageUtils.java
/** * Method for checking if the message has attachments. *//*from w ww . j a v a 2 s . c o m*/ public static List<MimePart> attachmentsFromPart(Part part) throws MessagingException, IOException { List<MimePart> attachmentParts = new ArrayList<MimePart>(); if (part instanceof MimePart) { MimePart mimePart = (MimePart) part; if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) mimePart.getContent(); for (int i = 0; i < mp.getCount(); i++) { MimeBodyPart bodyPart = (MimeBodyPart) mp.getBodyPart(i); if (Part.ATTACHMENT.equals(bodyPart.getDisposition()) || Part.INLINE.equals(bodyPart.getDisposition())) { attachmentParts.add(bodyPart); } } } else if (part.isMimeType("application/*")) { attachmentParts.add(mimePart); } } return attachmentParts; }
From source file:com.stimulus.archiva.extraction.MessageExtraction.java
private static void dumpPart(Part p, Hashtable<String, Part> attachments, Hashtable<String, String> inlines, Hashtable<String, String> images, Hashtable<String, String> nonImages, ArrayList<String> mimeTypes, String subject) throws Exception { mimeTypes.add(p.getContentType());/* ww w .java 2 s .co m*/ if (!p.isMimeType("multipart/*")) { String disp = p.getDisposition(); String fname = p.getFileName(); if (fname != null) { try { fname = MimeUtility.decodeText(fname); } catch (Exception e) { logger.debug("cannot decode filename:" + e.getMessage()); } } if ((disp != null && Compare.equalsIgnoreCase(disp, Part.ATTACHMENT)) || fname != null) { String filename = getFilename(subject, p); if (!filename.equalsIgnoreCase("winmail.dat")) { attachments.put(filename, p); } /*if (p.isMimeType("image/*")) { String str[] = p.getHeader("Content-ID"); if (str != null) images.put(filename,str[0]); else images.put(filename, filename); } return;*/ } } if (p.isMimeType("text/plain")) { String str = ""; if (inlines.containsKey("text/plain")) str = (String) inlines.get("text/plain") + "\n\n-------------------------------\n"; inlines.put("text/plain", str + getTextContent(p)); } else if (p.isMimeType("text/html")) { inlines.put("text/html", getTextContent(p)); } else if (p.isMimeType("text/xml")) { attachments.put(getFilename(subject, p), p); } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) dumpPart(mp.getBodyPart(i), attachments, inlines, images, nonImages, mimeTypes, subject); } else if (p.isMimeType("message/rfc822")) { dumpPart((Part) p.getContent(), attachments, inlines, images, nonImages, mimeTypes, subject); } else if (p.isMimeType("application/ms-tnef")) { Part tnefpart = TNEFMime.convert(null, p, false); if (tnefpart != null) { dumpPart((Part) tnefpart, attachments, inlines, images, nonImages, mimeTypes, subject); } } else if (p.isMimeType("application/*")) { String filename = getFilename("application", p); attachments.put(filename, p); String str[] = p.getHeader("Content-ID"); if (str != null) nonImages.put(filename, str[0]); } else if (p.isMimeType("image/*")) { String fileName = getFilename("image", p); attachments.put(fileName, p); String str[] = p.getHeader("Content-ID"); if (str != null) images.put(fileName, str[0]); else images.put(fileName, fileName); } else { String contentType = p.getContentType(); Object o = p.getContent(); if (o instanceof String) { String str = ""; if (inlines.containsKey("text/plain")) str = (String) inlines.get("text/plain") + "\n\n-------------------------------\n"; inlines.put(contentType, str + (String) o); } else { String fileName = getFilenameFromContentType("attach", contentType); attachments.put(fileName, p); } } }
From source file:com.aurel.track.util.emailHandling.MailReader.java
/** * Process multipart e-mail// w w w .j a v a 2 s . com */ private static String handleMultipart(Part part, List<EmailAttachment> attachments, boolean ignoreAttachments) throws MessagingException, IOException { if (part.isMimeType("multipart/alternative")) { return hadleMultipartAlternative(part, attachments, ignoreAttachments); } else if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < mp.getCount(); i++) { StringBuffer s = handlePart(mp.getBodyPart(i), attachments, ignoreAttachments); if (s != null) { sb.append(s); } } return sb.toString(); } return null; }
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// w ww . ja v a 2s.c om * @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; }