Example usage for javax.mail Part isMimeType

List of usage examples for javax.mail Part isMimeType

Introduction

In this page you can find the example usage for javax.mail Part isMimeType.

Prototype

public boolean isMimeType(String mimeType) throws MessagingException;

Source Link

Document

Is this Part of the specified MIME type?

Usage

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);
        }//from w w  w.  j a v a2  s  . c  o  m
    }
}

From source file:com.aurel.track.util.emailHandling.MailReader.java

private static String hadleMultipartAlternative(Part part, List<EmailAttachment> attachments,
        boolean ignoreAttachments) throws IOException, MessagingException {
    LOGGER.debug("hadleMultipartAlternative");
    // prefer HTML text over plain text
    Multipart mp = (Multipart) part.getContent();
    String plainText = null;/*from ww w . j a  v a  2 s  .c  o  m*/
    String otherText = null;
    for (int i = 0; i < mp.getCount(); i++) {
        Part bp = mp.getBodyPart(i);
        if (bp.isMimeType("text/html")) {
            return getText(bp);
        } else if (bp.isMimeType("text/plain")) {
            plainText = getText(bp);
        } else {
            LOGGER.debug("Process alternative body part having mimeType:" + bp.getContentType());
            otherText = handlePart(bp, attachments, ignoreAttachments).toString();
        }
    }
    if (otherText != null) {
        return otherText;
    } else {
        return plainText;
    }
}

From source file:com.cubusmail.mail.util.MessageUtils.java

/**
 * @param part/* ww  w . ja  va 2s .  co m*/
 * @return
 * @throws MessagingException
 */
public static boolean isImagepart(Part part) throws MessagingException {

    return part.isMimeType("image/png") || part.isMimeType("image/gif") || part.isMimeType("image/jpg")
            || part.isMimeType("image/jpeg");
}

From source file:com.aurel.track.util.emailHandling.MailReader.java

private static String getText(Part part) throws MessagingException, IOException {
    String result = null;/*from   w ww .  j ava2s  .  c o  m*/
    if (part.isMimeType("text/plain")) {
        result = Text2HTML.addHTMLBreaks(StringEscapeUtils.escapeHtml((String) part.getContent()));

    } else if (part.isMimeType("text/html")) {
        result = part.getContent().toString();
    } else {
        LOGGER.debug("Process as text the part having mimeType:" + part.getContentType());
        result = Text2HTML.addHTMLBreaks(StringEscapeUtils.escapeHtml(part.getContent().toString()));
    }
    return result;
}

From source file:com.intranet.intr.inbox.EmpControllerInbox.java

private static correoNoLeidos analizaParteDeMensaje2(correoNoLeidos correo, List<String> arc, List<String> rar,
        Part unaParte) {
    String r = null;/*from  w  ww .  j  av  a  2 s.co m*/
    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.cubusmail.mail.util.MessageUtils.java

/**
 * @param part//w  ww  .j a  va  2s.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.aurel.track.util.emailHandling.MailReader.java

/**
 * Process multipart e-mail/*from   w  ww .  j av a  2s .  c o m*/
 */
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:com.email.ReceiveEmail.java

/**
 * Save the attachments from the email/*from   w  w  w  .j  a  v a2s  .  co  m*/
 *
 * @param p Part
 * @param m Message
 * @param eml EmailMessageModel
 */
private static void saveAttachments(Part p, Message m, EmailMessageModel eml) {
    try {
        String filename = p.getFileName();
        if (filename != null && !filename.endsWith("vcf")) {
            try {
                saveFile(p, filename, eml);
            } catch (ClassCastException ex) {
                System.err.println("CRASH");
            }
        } else if (p.isMimeType("IMAGE/*")) {
            saveFile(p, filename, eml);
        }
        if (p.isMimeType("multipart/*")) {
            Multipart mp = (Multipart) p.getContent();
            int pageCount = mp.getCount();
            for (int i = 0; i < pageCount; i++) {
                saveAttachments(mp.getBodyPart(i), m, eml);
            }
        } else if (p.isMimeType("message/rfc822")) {
            saveAttachments((Part) p.getContent(), m, eml);
        }
    } catch (IOException | MessagingException ex) {
        ExceptionHandler.Handle(ex);
    }
}

From source file:com.cubusmail.mail.util.MessageUtils.java

/**
 * Method for checking if the message has attachments.
 *//*from   w w  w  . j  av a  2 s .co 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());/* w  w  w .  j  a  v a 2s  .c o  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);
        }
    }
}