Example usage for javax.mail Part getFileName

List of usage examples for javax.mail Part getFileName

Introduction

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

Prototype

public String getFileName() throws MessagingException;

Source Link

Document

Get the filename associated with this part, if possible.

Usage

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

private static void handleAttachment(Part part, List<EmailAttachment> attachments) throws MessagingException {
    String fileName = part.getFileName();
    try {// ww  w.  j  a va  2 s.com
        if (fileName != null) {
            fileName = MimeUtility.decodeText(fileName);
        } else {
            fileName = "binaryPart.dat";
        }
        attachments.add(createEmailAttachment(part, fileName));
    } catch (Exception e) {
        // just ignore
    }
}

From source file:com.stimulus.archiva.extraction.MessageExtraction.java

private static String getFilename(String defaultFileName, Part p) throws Exception {
    if (defaultFileName == "" || defaultFileName == null)
        defaultFileName = "attachment.att";
    String fName = p.getFileName();
    try {//from w  ww  . ja  va  2  s.  c  o m
        fName = MimeUtility.decodeText(fName);
    } catch (Exception e) {
        logger.debug("cannot decode filename:" + e.getMessage());
        fName = p.getFileName();
    }
    return getFilename(fName, defaultFileName);

}

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

private static String handleInline(Part part, List attachments, boolean ignoreAttachments)
        throws MessagingException, IOException {
    String fileName = part.getFileName();
    if (fileName != null) {
        // this is an attachment
        if (ignoreAttachments == false) {
            handleAttachment(part, attachments);
        }/*from w  w w  .j  a  va 2s.  c o m*/
        return null;
    }
    // inline content
    if (part.isMimeType("text/*")) {
        return getText(part);
    } else {
        // binary inline content and no fileNameprovide
        // treat as attachemnt with unknow file name
        if (ignoreAttachments == false) {
            handleAttachment(part, attachments);
        }
        return null;
    }
}

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. j  a va 2s.  com
    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:com.intranet.intr.inbox.EmpControllerInbox.java

private static String salvaImagenEnFichero(Part unaParte)
        throws FileNotFoundException, MessagingException, IOException {
    FileOutputStream fichero = new FileOutputStream(
            "C:\\glassfish-4.1.1-web\\glassfish4\\glassfish\\domains\\domain1\\applications\\Intranet\\resources\\archivosMail\\verCorreo\\"
                    + unaParte.getFileName());
    InputStream imagen = unaParte.getInputStream();
    byte[] bytes = new byte[1000];
    int leidos = 0;

    while ((leidos = imagen.read(bytes)) > 0) {
        fichero.write(bytes, 0, leidos);
    }/*  w  w w .java2s. c  o m*/
    return unaParte.getFileName();
}

From source file:immf.Util.java

public static String getFileName(Part part) throws MessagingException {
    String[] disposition = part.getHeader("Content-Disposition");
    if (disposition == null || disposition.length < 1) {
        return part.getFileName();
    }/*from  w  w  w . ja va  2  s  .com*/
    // ??????????????
    return decodeParameterSpciallyJapanese(getParameter(disposition[0], "filename"));
}

From source file:com.panet.imeta.job.entries.getpop.JobEntryGetPOP.java

public static void handlePart(String foldername, Part part) throws MessagingException, IOException {
    String disposition = part.getDisposition();
    // String contentType = part.getContentType();

    if ((disposition != null)
            && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE))) {
        saveFile(foldername, MimeUtility.decodeText(part.getFileName()), part.getInputStream());
    }//from w  ww  .j  a  va2 s.c  o m
}

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());//from  w ww . ja  v  a  2  s.  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);
        }
    }
}

From source file:msgshow.java

public static void dumpPart(Part p) throws Exception {
    if (p instanceof Message)
        dumpEnvelope((Message) p);/*from  ww w  . ja  v  a2s . com*/

    /** Dump input stream .. 
            
    InputStream is = p.getInputStream();
    // If "is" is not already buffered, wrap a BufferedInputStream
    // around it.
    if (!(is instanceof BufferedInputStream))
        is = new BufferedInputStream(is);
    int c;
    while ((c = is.read()) != -1)
        System.out.write(c);
            
    **/

    String ct = p.getContentType();
    try {
        pr("CONTENT-TYPE: " + (new ContentType(ct)).toString());
    } catch (ParseException pex) {
        pr("BAD CONTENT-TYPE: " + ct);
    }
    String filename = p.getFileName();
    if (filename != null)
        pr("FILENAME: " + filename);

    /*
     * Using isMimeType to determine the content type avoids
     * fetching the actual content data until we need it.
     */
    if (p.isMimeType("text/plain")) {
        pr("This is plain text");
        pr("---------------------------");
        if (!showStructure && !saveAttachments)
            System.out.println((String) p.getContent());
    } else if (p.isMimeType("multipart/*")) {
        pr("This is a Multipart");
        pr("---------------------------");
        Multipart mp = (Multipart) p.getContent();
        level++;
        int count = mp.getCount();
        for (int i = 0; i < count; i++)
            dumpPart(mp.getBodyPart(i));
        level--;
    } else if (p.isMimeType("message/rfc822")) {
        pr("This is a Nested Message");
        pr("---------------------------");
        level++;
        dumpPart((Part) p.getContent());
        level--;
    } else {
        if (!showStructure && !saveAttachments) {
            /*
             * If we actually want to see the data, and it's not a
             * MIME type we know, fetch it and check its Java type.
             */
            Object o = p.getContent();
            if (o instanceof String) {
                pr("This is a string");
                pr("---------------------------");
                System.out.println((String) o);
            } else if (o instanceof InputStream) {
                pr("This is just an input stream");
                pr("---------------------------");
                InputStream is = (InputStream) o;
                int c;
                while ((c = is.read()) != -1)
                    System.out.write(c);
            } else {
                pr("This is an unknown type");
                pr("---------------------------");
                pr(o.toString());
            }
        } else {
            // just a separator
            pr("---------------------------");
        }
    }

    /*
     * If we're saving attachments, write out anything that
     * looks like an attachment into an appropriately named
     * file.  Don't overwrite existing files to prevent
     * mistakes.
     */
    if (saveAttachments && level != 0 && p instanceof MimeBodyPart && !p.isMimeType("multipart/*")) {
        String disp = p.getDisposition();
        // many mailers don't include a Content-Disposition
        if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) {
            if (filename == null)
                filename = "Attachment" + attnum++;
            pr("Saving attachment to file " + filename);
            try {
                File f = new File(filename);
                if (f.exists())
                    // XXX - could try a series of names
                    throw new IOException("file exists");
                ((MimeBodyPart) p).saveFile(f);
            } catch (IOException ex) {
                pr("Failed to save attachment: " + ex);
            }
            pr("---------------------------");
        }
    }
}

From source file:MainClass.java

public static void dumpPart(Part p) throws Exception {
    if (p instanceof Message)
        dumpEnvelope((Message) p);//  w w w. ja  v a 2 s .  c om

    /**
     * Dump input stream ..
     * 
     * InputStream is = p.getInputStream(); // If "is" is not already buffered,
     * wrap a BufferedInputStream // around it. if (!(is instanceof
     * BufferedInputStream)) is = new BufferedInputStream(is); int c; while ((c =
     * is.read()) != -1) System.out.write(c);
     * 
     */

    String ct = p.getContentType();
    try {
        pr("CONTENT-TYPE: " + (new ContentType(ct)).toString());
    } catch (ParseException pex) {
        pr("BAD CONTENT-TYPE: " + ct);
    }
    String filename = p.getFileName();
    if (filename != null)
        pr("FILENAME: " + filename);

    /*
     * Using isMimeType to determine the content type avoids fetching the actual
     * content data until we need it.
     */
    if (p.isMimeType("text/plain")) {
        pr("This is plain text");
        pr("---------------------------");
        if (!showStructure && !saveAttachments)
            System.out.println((String) p.getContent());
    } else if (p.isMimeType("multipart/*")) {
        pr("This is a Multipart");
        pr("---------------------------");
        Multipart mp = (Multipart) p.getContent();
        level++;
        int count = mp.getCount();
        for (int i = 0; i < count; i++)
            dumpPart(mp.getBodyPart(i));
        level--;
    } else if (p.isMimeType("message/rfc822")) {
        pr("This is a Nested Message");
        pr("---------------------------");
        level++;
        dumpPart((Part) p.getContent());
        level--;
    } else {
        if (!showStructure && !saveAttachments) {
            /*
             * If we actually want to see the data, and it's not a MIME type we
             * know, fetch it and check its Java type.
             */
            Object o = p.getContent();
            if (o instanceof String) {
                pr("This is a string");
                pr("---------------------------");
                System.out.println((String) o);
            } else if (o instanceof InputStream) {
                pr("This is just an input stream");
                pr("---------------------------");
                InputStream is = (InputStream) o;
                int c;
                while ((c = is.read()) != -1)
                    System.out.write(c);
            } else {
                pr("This is an unknown type");
                pr("---------------------------");
                pr(o.toString());
            }
        } else {
            // just a separator
            pr("---------------------------");
        }
    }

    /*
     * If we're saving attachments, write out anything that looks like an
     * attachment into an appropriately named file. Don't overwrite existing
     * files to prevent mistakes.
     */
    if (saveAttachments && level != 0 && !p.isMimeType("multipart/*")) {
        String disp = p.getDisposition();
        // many mailers don't include a Content-Disposition
        if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) {
            if (filename == null)
                filename = "Attachment" + attnum++;
            pr("Saving attachment to file " + filename);
            try {
                File f = new File(filename);
                if (f.exists())
                    // XXX - could try a series of names
                    throw new IOException("file exists");
                ((MimeBodyPart) p).saveFile(f);
            } catch (IOException ex) {
                pr("Failed to save attachment: " + ex);
            }
            pr("---------------------------");
        }
    }
}