Example usage for javax.mail Part getContent

List of usage examples for javax.mail Part getContent

Introduction

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

Prototype

public Object getContent() throws IOException, MessagingException;

Source Link

Document

Return the content as a Java object.

Usage

From source file:search.java

public static void dumpPart(Part p) throws Exception {
    if (p instanceof Message) {
        Message m = (Message) p;//w  w  w  .j av a2  s.  c  o  m
        Address[] a;
        // FROM 
        if ((a = m.getFrom()) != null) {
            for (int j = 0; j < a.length; j++)
                System.out.println("FROM: " + a[j].toString());
        }

        // TO
        if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
            for (int j = 0; j < a.length; j++)
                System.out.println("TO: " + a[j].toString());
        }

        // SUBJECT
        System.out.println("SUBJECT: " + m.getSubject());

        // DATE
        Date d = m.getSentDate();
        System.out.println("SendDate: " + (d != null ? d.toLocaleString() : "UNKNOWN"));

        // FLAGS:
        Flags flags = m.getFlags();
        StringBuffer sb = new StringBuffer();
        Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags

        boolean first = true;
        for (int i = 0; i < sf.length; i++) {
            String s;
            Flags.Flag f = sf[i];
            if (f == Flags.Flag.ANSWERED)
                s = "\\Answered";
            else if (f == Flags.Flag.DELETED)
                s = "\\Deleted";
            else if (f == Flags.Flag.DRAFT)
                s = "\\Draft";
            else if (f == Flags.Flag.FLAGGED)
                s = "\\Flagged";
            else if (f == Flags.Flag.RECENT)
                s = "\\Recent";
            else if (f == Flags.Flag.SEEN)
                s = "\\Seen";
            else
                continue; // skip it
            if (first)
                first = false;
            else
                sb.append(' ');
            sb.append(s);
        }

        String[] uf = flags.getUserFlags(); // get the user flag strings
        for (int i = 0; i < uf.length; i++) {
            if (first)
                first = false;
            else
                sb.append(' ');
            sb.append(uf[i]);
        }
        System.out.println("FLAGS = " + sb.toString());
    }

    System.out.println("CONTENT-TYPE: " + p.getContentType());

    /* Dump input stream
    InputStream is = ((MimeMessage)m).getInputStream();
    int c;
    while ((c = is.read()) != -1)
        System.out.write(c);
    */

    Object o = p.getContent();
    if (o instanceof String) {
        System.out.println("This is a String");
        System.out.println((String) o);
    } else if (o instanceof Multipart) {
        System.out.println("This is a Multipart");
        Multipart mp = (Multipart) o;
        int count = mp.getCount();
        for (int i = 0; i < count; i++)
            dumpPart(mp.getBodyPart(i));
    } else if (o instanceof InputStream) {
        System.out.println("This is just an input stream");
        InputStream is = (InputStream) o;
        int c;
        while ((c = is.read()) != -1)
            System.out.write(c);
    }
}

From source file:org.sakaiproject.james.SakaiMailet.java

/**
 * Breaks email messages into parts which can be saved as files (saves as attachments) or viewed as plain text (added to body of message).
 * //ww w . j  a va2  s  .  c  o  m
 * @param siteId
 *        Site associated with attachments, if any
 * @param p
 *        The message-part embedded in a message..
 * @param id
 *        The string containing the message's id.
 * @param bodyBuf
 *        The string-buffers in which the plain/text and/or html/text message body is being built.
 * @param bodyContentType
 *        The value of the Content-Type header for the mesage body.
 * @param attachments
 *        The ReferenceVector in which references to attachments are collected.
 * @param embedCount
 *        An Integer that counts embedded messages (outer message is zero).
 * @return Value of embedCount (updated if this call processed any embedded messages).
 */
protected Integer parseParts(String siteId, Part p, String id, StringBuilder bodyBuf[],
        StringBuilder bodyContentType, List attachments, Integer embedCount)
        throws MessagingException, IOException {
    // increment embedded message counter
    if (p instanceof Message) {
        embedCount = Integer.valueOf(embedCount.intValue() + 1);
    }

    String type = p.getContentType();

    // discard if content-type is unknown
    if (type == null || "".equals(type)) {
        M_log.warn(this + " message with unknown content-type discarded");
    }

    // add plain text to bodyBuf[0]
    else if (p.isMimeType("text/plain") && p.getFileName() == null) {
        Object o = null;
        // let them convert to text if possible
        // but if bad encaps get the stream and do it ourselves
        try {
            o = p.getContent();
        } catch (java.io.UnsupportedEncodingException ignore) {
            o = p.getInputStream();
        }

        String txt = null;
        String innerContentType = p.getContentType();

        if (o instanceof String) {
            txt = (String) p.getContent();
            if (bodyContentType != null && bodyContentType.length() == 0)
                bodyContentType.append(innerContentType);
        }

        else if (o instanceof InputStream) {
            InputStream in = (InputStream) o;
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[in.available()];
            for (int len = in.read(buf); len != -1; len = in.read(buf))
                out.write(buf, 0, len);
            String charset = (new ContentType(innerContentType)).getParameter("charset");
            // RFC 2045 says if no char set specified use US-ASCII.
            // If specified but illegal that's less clear. The common case is X-UNKNOWN.
            // my sense is that UTF-8 is most likely these days but the sample we got
            // was actually ISO 8859-1. Could also justify using US-ASCII. Duh...
            if (charset == null)
                charset = "us-ascii";
            try {
                txt = out.toString(MimeUtility.javaCharset(charset));
            } catch (java.io.UnsupportedEncodingException ignore) {
                txt = out.toString("UTF-8");
            }
            if (bodyContentType != null && bodyContentType.length() == 0)
                bodyContentType.append(innerContentType);
        }

        // remove extra line breaks added by mac Mail, perhaps others
        // characterized by a space followed by a line break
        if (txt != null) {
            txt = txt.replaceAll(" \n", " ");
        }

        // make sure previous message parts ended with newline
        if (bodyBuf[0].length() > 0 && bodyBuf[0].charAt(bodyBuf[0].length() - 1) != '\n')
            bodyBuf[0].append("\n");

        bodyBuf[0].append(txt);
    }

    // add html text to bodyBuf[1]
    else if (p.isMimeType("text/html") && p.getFileName() == null) {
        Object o = null;
        // let them convert to text if possible
        // but if bad encaps get the stream and do it ourselves
        try {
            o = p.getContent();
        } catch (java.io.UnsupportedEncodingException ignore) {
            o = p.getInputStream();
        }
        String txt = null;
        String innerContentType = p.getContentType();

        if (o instanceof String) {
            txt = (String) p.getContent();
            if (bodyContentType != null && bodyContentType.length() == 0)
                bodyContentType.append(innerContentType);
        }

        else if (o instanceof InputStream) {
            InputStream in = (InputStream) o;
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[in.available()];
            for (int len = in.read(buf); len != -1; len = in.read(buf))
                out.write(buf, 0, len);
            String charset = (new ContentType(innerContentType)).getParameter("charset");
            if (charset == null)
                charset = "us-ascii";
            try {
                txt = out.toString(MimeUtility.javaCharset(charset));
            } catch (java.io.UnsupportedEncodingException ignore) {
                txt = out.toString("UTF-8");
            }
            if (bodyContentType != null && bodyContentType.length() == 0)
                bodyContentType.append(innerContentType);
        }

        // remove bad image tags and naughty javascript
        if (txt != null) {
            txt = Web.cleanHtml(txt);
        }

        bodyBuf[1].append(txt);
    }

    // process subparts of multiparts
    else if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        int count = mp.getCount();
        for (int i = 0; i < count; i++) {
            embedCount = parseParts(siteId, mp.getBodyPart(i), id, bodyBuf, bodyContentType, attachments,
                    embedCount);
        }
    }

    // Discard parts with mime-type application/applefile. If an e-mail message contains an attachment is sent from
    // a macintosh, you may get two parts, one for the data fork and one for the resource fork. The part that
    // corresponds to the resource fork confuses users, this has mime-type application/applefile. The best thing
    // is to discard it.
    else if (p.isMimeType("application/applefile")) {
        M_log.warn(this + " message with application/applefile discarded");
    }

    // discard enriched text version of the message.
    // Sakai only uses the plain/text or html/text version of the message.
    else if (p.isMimeType("text/enriched") && p.getFileName() == null) {
        M_log.warn(this + " message with text/enriched discarded");
    }

    // everything else gets treated as an attachment
    else {
        String name = p.getFileName();

        // look for filenames not parsed by getFileName() 
        if (name == null && type.indexOf(NAME_PREFIX) != -1) {
            name = type.substring(type.indexOf(NAME_PREFIX) + NAME_PREFIX.length());
        }
        // ContentType can't handle filenames with spaces or UTF8 characters
        if (name != null) {
            String decodedName = MimeUtility.decodeText(name); // first decode RFC 2047
            type = type.replace(name, URLEncoder.encode(decodedName, "UTF-8"));
            name = decodedName;
        }

        ContentType cType = new ContentType(type);
        String disposition = p.getDisposition();
        int approxSize = p.getSize();

        if (name == null) {
            name = "unknown";
            // if file's parent is multipart/alternative,
            // provide a better name for the file
            if (p instanceof BodyPart) {
                Multipart parent = ((BodyPart) p).getParent();
                if (parent != null) {
                    String pType = parent.getContentType();
                    ContentType pcType = new ContentType(pType);
                    if (pcType.getBaseType().equalsIgnoreCase("multipart/alternative")) {
                        name = "message" + embedCount;
                    }
                }
            }
            if (p.isMimeType("text/html")) {
                name += ".html";
            } else if (p.isMimeType("text/richtext")) {
                name += ".rtx";
            } else if (p.isMimeType("text/rtf")) {
                name += ".rtf";
            } else if (p.isMimeType("text/enriched")) {
                name += ".etf";
            } else if (p.isMimeType("text/plain")) {
                name += ".txt";
            } else if (p.isMimeType("text/xml")) {
                name += ".xml";
            } else if (p.isMimeType("message/rfc822")) {
                name += ".txt";
            }
        }

        // read the attachments bytes, and create it as an attachment in content hosting
        byte[] bodyBytes = readBody(approxSize, p.getInputStream());
        if ((bodyBytes != null) && (bodyBytes.length > 0)) {
            // can we ignore the attachment it it's just whitespace chars??
            Reference attachment = createAttachment(siteId, attachments, cType.getBaseType(), name, bodyBytes,
                    id);

            // add plain/text attachment reference (if plain/text message)
            if (attachment != null && bodyBuf[0].length() > 0)
                bodyBuf[0]
                        .append("[see attachment: \"" + name + "\", size: " + bodyBytes.length + " bytes]\n\n");

            // add html/text attachment reference (if html/text message)
            if (attachment != null && bodyBuf[1].length() > 0)
                bodyBuf[1].append(
                        "<p>[see attachment: \"" + name + "\", size: " + bodyBytes.length + " bytes]</p>");

            // add plain/text attachment reference (if no plain/text and no html/text)
            if (attachment != null && bodyBuf[0].length() == 0 && bodyBuf[1].length() == 0)
                bodyBuf[0]
                        .append("[see attachment: \"" + name + "\", size: " + bodyBytes.length + " bytes]\n\n");
        }
    }

    return embedCount;
}

From source file:org.socraticgrid.displaymaildata.DisplayMailDataHandler.java

/**
 * Extract/format mail message content.//from  w w w  .  j  a v a2 s.c  om
 * 
 * @param msg
 * @return
 */
private String fetchMsgContent(Message msg) throws Exception {

    String content = null;
    String html = null;
    String text = null;

    if (msg.isMimeType("multipart/*")) {

        Multipart mp = (Multipart) msg.getContent();
        // the content was not fetched from the server

        // parse each Part
        for (int i = 0; i < mp.getCount(); i++) {
            Part inner_part = mp.getBodyPart(i);

            if (inner_part.isMimeType("text/plain")) {
                text = (String) inner_part.getContent();
                System.out.println("TEXT=\n" + text);

            } else if (inner_part.isMimeType("text/html")) {
                html = (String) inner_part.getContent();
                System.out.println("HTML=\n" + content);

            }
        }
    } else if (msg.isMimeType("text/plain")) {
        text = (String) msg.getContent();
        System.out.println("TEXT only=\n" + text);
    }
    if (!CommonUtil.strNullorEmpty(html)) {
        content = html;
    } else {
        content = text;
    }
    return content;
}

From source file:org.nuxeo.ecm.platform.mail.listener.action.ExtractMessageInformationAction.java

protected void getAttachmentParts(Part part, String defaultFilename, MimetypeRegistry mimeService,
        ExecutionContext context) throws MessagingException, IOException {
    String filename = getFilename(part, defaultFilename);
    List<Blob> blobs = (List<Blob>) context.get(ATTACHMENTS_KEY);

    if (part.isMimeType("multipart/alternative")) {
        bodyContent += getText(part);//w  w w. ja v  a 2s .  c  o m
    } else {
        if (!part.isMimeType("multipart/*")) {
            String disp = part.getDisposition();
            // no disposition => mail body, which can be also blob (image for
            // instance)
            if (disp == null && // convert only text
                    part.getContentType().toLowerCase().startsWith("text/")) {
                bodyContent += decodeMailBody(part);
            } else {
                Blob blob;
                try (InputStream in = part.getInputStream()) {
                    blob = Blobs.createBlob(in);
                }
                String mime = DEFAULT_BINARY_MIMETYPE;
                try {
                    if (mimeService != null) {
                        ContentType contentType = new ContentType(part.getContentType());
                        mime = mimeService.getMimetypeFromFilenameAndBlobWithDefault(filename, blob,
                                contentType.getBaseType());
                    }
                } catch (MessagingException | MimetypeDetectionException e) {
                    log.error(e);
                }
                blob.setMimeType(mime);

                blob.setFilename(filename);

                blobs.add(blob);
            }
        }

        if (part.isMimeType("multipart/*")) {
            // This is a Multipart
            Multipart mp = (Multipart) part.getContent();

            int count = mp.getCount();
            for (int i = 0; i < count; i++) {
                getAttachmentParts(mp.getBodyPart(i), defaultFilename, mimeService, context);
            }
        } else if (part.isMimeType(MESSAGE_RFC822_MIMETYPE)) {
            // This is a Nested Message
            getAttachmentParts((Part) part.getContent(), defaultFilename, mimeService, context);
        }
    }

}

From source file:edu.stanford.muse.email.EmailFetcherStats.java

/**
 * this method returns the text content of the message as a list of strings
 * // each element of the list could be the content of a multipart message
 * // m is the top level subject//from w  w  w  .j  a v  a2  s  .c  o  m
 * // p is the specific part that we are processing (p could be == m)
 * also sets up names of attachments (though it will not download the
 * attachment unless downloadAttachments is true)
 */
private List<String> processMessagePart(int messageNum, Message m, Part p, List<Blob> attachmentsList)
        throws MessagingException, IOException {
    List<String> list = new ArrayList<String>(); // return list
    if (p == null) {
        dataErrors.add("part is null: " + folder_name() + " idx " + messageNum);
        return list;
    }

    if (p == m && p.isMimeType("text/html")) {
        /*
        String s = "top level part is html! message:" + m.getSubject() + " " + m.getDescription();
        dataErrors.add(s);
        */
        // we don't normally expect the top-level part to have content-type text/html
        // but we saw this happen on some sample archives pst -> emailchemy. so allow it and handle it by parsing the html
        String html = (String) p.getContent();
        String text = Util.unescapeHTML(html);
        org.jsoup.nodes.Document doc = Jsoup.parse(text);

        StringBuilder sb = new StringBuilder();
        HTMLUtils.extractTextFromHTML(doc.body(), sb);
        list.add(sb.toString());
        return list;
    }

    if (p.isMimeType("text/plain")) {
        //make sure, p is not wrongly labelled as plain text.
        Enumeration headers = p.getAllHeaders();
        boolean dirty = false;
        if (headers != null)
            while (headers.hasMoreElements()) {
                Header h = (Header) headers.nextElement();
                String name = h.getName();
                String value = h.getValue();
                if (name != null && value != null) {
                    if (name.equals("Content-transfer-encoding") && value.equals("base64")) {
                        dirty = true;
                        break;
                    }
                }
            }
        String fname = p.getFileName();
        if (fname != null) {
            int idx = fname.lastIndexOf('.');
            if ((idx < fname.length()) && (idx >= 0)) {
                String extension = fname.substring(idx);
                //anything extension other than .txt is suspicious.
                if (!extension.equals(".txt"))
                    dirty = true;
            }
        }
        if (dirty) {
            dataErrors.add("Dirty message part, has conflicting message part headers." + folder_name()
                    + " Message# " + messageNum);
            return list;
        }

        log.debug("Message part with content type text/plain");
        String content;
        String type = p.getContentType(); // new InputStreamReader(p.getInputStream(), "UTF-8");
        try {
            // if forced encoding is set, we read the string with that encoding, otherwise we just use whatever p.getContent gives us
            if (FORCED_ENCODING != null) {
                byte b[] = Util.getBytesFromStream(p.getInputStream());
                content = new String(b, FORCED_ENCODING);
            } else
                content = (String) p.getContent();
        } catch (UnsupportedEncodingException uee) {
            dataErrors.add("Unsupported encoding: " + folder_name() + " Message #" + messageNum + " type "
                    + type + ", using brute force conversion");
            // a particularly nasty issue:javamail can't handle utf-7 encoding which is common with hotmail and exchange servers.
            // we're using the workaround suggested on this page: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4304013
            // though it may be better to consider official support for utf-7 or other encodings.

            // TOFIX: I get an exception for utfutf8-encoding which has a base64 encoding embedded on it.
            // Unsupported encoding: gmail-sent Message #10477 type text/plain; charset=x-utf8utf8; name="newyorker.txt",
            // the hack below doesn't work for it.
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            p.writeTo(bao);
            content = bao.toString();
        }
        list.add(content);
    } else if (p.isMimeType("multipart/*") || p.isMimeType("message/rfc822")) {
        // rfc822 mime type is for embedded mbox format or some such (appears for things like
        // forwarded messages). the content appears to be just a multipart.
        Object o = p.getContent();
        if (o instanceof Multipart) {
            Multipart allParts = (Multipart) o;
            if (p.isMimeType("multipart/alternative")) {
                // this is an alternative mime type. v common case to have text and html alternatives
                // so just process the text part if there is one, and avoid fetching the alternatives.
                // useful esp. because many ordinary messages are alternative: text and html and we don't want to fetch the html.
                // revisit in future we want to retain the html alternative for display purposes
                Part[] parts = new Part[allParts.getCount()];
                for (int i = 0; i < parts.length; i++)
                    parts[i] = allParts.getBodyPart(i);

                for (int i = 0; i < parts.length; i++) {
                    Part thisPart = parts[i];
                    if (thisPart.isMimeType("text/plain")) {
                        // common case, return quickly
                        list.add((String) thisPart.getContent());
                        log.debug("Multipart/alternative with content type text/plain");
                        return list;
                    }
                }

                // no text part, let's look for an html part. this happens for html parts.
                for (int i = 0; i < allParts.getCount(); i++) {
                    Part thisPart = parts[i];
                    if (thisPart.isMimeType("text/html")) {
                        // common case, return quickly
                        String html = (String) thisPart.getContent();
                        String text = Util.unescapeHTML(html);
                        org.jsoup.nodes.Document doc = Jsoup.parse(text);

                        StringBuilder sb = new StringBuilder();
                        HTMLUtils.extractTextFromHTML(doc.body(), sb);
                        list.add(sb.toString());

                        log.debug("Multipart/alternative with content type text/html");
                        return list;
                    }
                }

                // no text or html part. hmmm... blindly process the first part only
                if (allParts.getCount() >= 1)
                    list.addAll(processMessagePart(messageNum, m, allParts.getBodyPart(0), attachmentsList));
            } else {
                // process it like a regular multipart
                for (int i = 0; i < allParts.getCount(); i++) {
                    BodyPart bp = allParts.getBodyPart(i);
                    list.addAll(processMessagePart(messageNum, m, bp, attachmentsList));
                }
            }
        } else if (o instanceof Part)
            list.addAll(processMessagePart(messageNum, m, (Part) o, attachmentsList));
        else
            dataErrors.add("Unhandled part content, " + folder_name() + " Message #" + messageNum
                    + "Java type: " + o.getClass() + " Content-Type: " + p.getContentType());
    } else {
        try {
            // do attachments only if downloadAttachments is set.
            // some apps do not need attachments, so this saves some time.
            // however, it seems like a lot of time is taken in imap prefetch, which gets attachments too?
            if (fetchConfig.downloadAttachments)
                handleAttachments(messageNum, m, p, list, attachmentsList);
        } catch (Exception e) {
            dataErrors.add("Ignoring attachment for " + folder_name() + " Message #" + messageNum + ": "
                    + Util.stackTrace(e));
        }
    }

    return list;
}

From source file:org.campware.cream.modules.scheduledjobs.Pop3Job.java

private void saveAttachment(Part part, InboxEvent inboxentry) throws Exception {

    MimeBodyPart mbp = (MimeBodyPart) part;
    String fileName = mbp.getFileName();
    String fileType = mbp.getContentType();
    String fileId = mbp.getContentID();
    String fileEncoding = mbp.getEncoding();
    String attContent;/*from w  w w.  ja  v  a  2s . c  o m*/

    if (fileName == null || fileName.length() < 2) {
        fileName = new String("Unknown");
        if (fileType.indexOf("name") > 0) {
            int i = fileType.indexOf("name");
            int j = fileType.indexOf("\"", i + 1);
            if (j != -1) {
                int k = fileType.indexOf("\"", j + 1);
                if (k != -1) {
                    fileName = fileType.substring(j + 1, k);
                }

            } else {
                int k = fileType.indexOf(";", i + 1);
                if (k != -1) {
                    fileName = fileType.substring(i + 5, k);

                } else {
                    fileName = fileType.substring(i + 5, fileType.length());
                }

            }
        }
    }

    InboxAttachment entryItem = new InboxAttachment();

    entryItem.setFileName(fileName);
    if (fileType != null)
        entryItem.setContentType(fileType);

    if (mbp.getContent() instanceof InputStream) {
        InputStream is = new Base64.InputStream(mbp.getInputStream(), Base64.ENCODE);

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        StringBuffer att = new StringBuffer();
        String thisLine = reader.readLine();

        while (thisLine != null) {
            att.append(thisLine);
            thisLine = reader.readLine();
        }

        attContent = att.toString();
        //           MimeUtility.encode(part.getOutputStream(), "base64");
        //           attachments += saveFile(part.getFileName(), part.getInputStream());

    } else {
        attContent = part.getContent().toString();
    }

    entryItem.setContent(attContent);
    entryItem.setContentId(fileId);

    inboxentry.addInboxAttachment(entryItem);

}

From source file:com.zimbra.cs.mailbox.calendar.Invite.java

/**
 * Returns the meeting notes.  Meeting notes is the text/plain part in an
 * invite.  It typically includes CUA-generated meeting summary as well as
 * text entered by the user./*from   www . ja  va  2 s . c  o m*/
 *
 * @return null if notes is not found
 * @throws ServiceException
 */
public static String getDescription(Part mmInv, String mimeType) throws ServiceException {
    if (mmInv == null)
        return null;
    try {
        // If top-level is text/calendar, parse the iCalendar object and return
        // the DESCRIPTION of the first VEVENT/VTODO encountered.
        String mmCtStr = mmInv.getContentType();
        if (mmCtStr != null) {
            ContentType mmCt = new ContentType(mmCtStr);
            if (mmCt.match(MimeConstants.CT_TEXT_CALENDAR)) {
                boolean wantHtml = MimeConstants.CT_TEXT_HTML.equalsIgnoreCase(mimeType);
                Object mmInvContent = mmInv.getContent();
                InputStream is = null;
                try {
                    String charset = MimeConstants.P_CHARSET_UTF8;
                    if (mmInvContent instanceof InputStream) {
                        charset = mmCt.getParameter(MimeConstants.P_CHARSET);
                        if (charset == null)
                            charset = MimeConstants.P_CHARSET_UTF8;
                        is = (InputStream) mmInvContent;
                    } else if (mmInvContent instanceof String) {
                        String str = (String) mmInvContent;
                        charset = MimeConstants.P_CHARSET_UTF8;
                        is = new ByteArrayInputStream(str.getBytes(charset));
                    }
                    if (is != null) {
                        ZVCalendar iCal = ZCalendarBuilder.build(is, charset);
                        for (Iterator<ZComponent> compIter = iCal.getComponentIterator(); compIter.hasNext();) {
                            ZComponent component = compIter.next();
                            ICalTok compTypeTok = component.getTok();
                            if (compTypeTok == ICalTok.VEVENT || compTypeTok == ICalTok.VTODO) {
                                if (!wantHtml)
                                    return component.getPropVal(ICalTok.DESCRIPTION, null);
                                else
                                    return component.getDescriptionHtml();
                            }
                        }
                    }
                } finally {
                    ByteUtil.closeStream(is);
                }
            }
        }

        Object mmInvContent = mmInv.getContent();
        if (!(mmInvContent instanceof MimeMultipart)) {
            if (mmInvContent instanceof InputStream) {
                ByteUtil.closeStream((InputStream) mmInvContent);
            }
            return null;
        }
        MimeMultipart mm = (MimeMultipart) mmInvContent;

        // If top-level is multipart, get description from text/* part.
        int numParts = mm.getCount();
        String charset = null;
        for (int i = 0; i < numParts; i++) {
            BodyPart part = mm.getBodyPart(i);
            String ctStr = part.getContentType();
            try {
                ContentType ct = new ContentType(ctStr);
                if (ct.match(mimeType)) {
                    charset = ct.getParameter(MimeConstants.P_CHARSET);
                    if (charset == null)
                        charset = MimeConstants.P_CHARSET_DEFAULT;
                    byte[] descBytes = ByteUtil.getContent(part.getInputStream(), part.getSize());
                    return new String(descBytes, charset);
                }
                // If part is a multipart, recurse.
                if (ct.getBaseType().matches(MimeConstants.CT_MULTIPART_WILD)) {
                    String str = getDescription(part, mimeType);
                    if (str != null) {
                        return str;
                    }
                }
            } catch (javax.mail.internet.ParseException e) {
                ZimbraLog.calendar.warn("Invalid Content-Type found: \"" + ctStr + "\"; skipping part", e);
            }
        }
    } catch (IOException e) {
        throw ServiceException.FAILURE("Unable to get calendar item notes MIME part", e);
    } catch (MessagingException e) {
        throw ServiceException.FAILURE("Unable to get calendar item notes MIME part", e);
    }
    return null;
}

From source file:edu.stanford.muse.email.EmailFetcherStats.java

/**
 * recursively processes attachments, fetching and saving it if needed
 * parses the given part p, and adds it to hte attachmentsList.
 * in some cases, like a text/html type without a filename, we instead append it to the textlist
 * @throws MessagingException//from   www.ja  v  a 2s. co m
 */
private void handleAttachments(int idx, Message m, Part p, List<String> textList, List<Blob> attachmentsList)
        throws MessagingException {
    String ct = null;
    if (!(m instanceof MimeMessage)) {
        Exception e = new IllegalArgumentException("Not a MIME message!");
        e.fillInStackTrace();
        log.warn(Util.stackTrace(e));
        return;
    }

    String filename = null;
    try {
        filename = p.getFileName();
    } catch (Exception e) {
        // seen this happen with:
        // Folders__gmail-sent Message #12185 Expected ';', got "Message"
        // javax.mail.internet.ParseException: Expected ';', got "Message"

        dataErrors.add("Unable to read attachment name: " + folder_name() + " Message# " + idx);
        return;
    }

    String sanitizedFName = Util.sanitizeFolderName(emailStore.getAccountID() + "." + folder_name());
    if (filename == null) {
        String tempFname = sanitizedFName + "." + idx;
        dataErrors.add("attachment filename is null for " + sanitizedFName + " Message#" + idx
                + " assigning it the name: " + tempFname);
        if (p.isMimeType("text/html")) {
            try {
                log.info("Turning message " + sanitizedFName + " Message#" + idx
                        + " into text although it is an attachment");
                String html = (String) p.getContent();
                String text = Util.unescapeHTML(html);
                org.jsoup.nodes.Document doc = Jsoup.parse(text);

                StringBuilder sb = new StringBuilder();
                HTMLUtils.extractTextFromHTML(doc.body(), sb);
                textList.add(sb.toString());
                return;
            } catch (Exception e) {
                Util.print_exception("Error reading contents of text/html multipart without a filename!", e,
                        log);
                return;
            }
        }
        filename = tempFname;
    }

    // Replacing any of the disallowed filename characters (\/:*?"<>|&) to _
    // (note: & causes problems with URLs for serveAttachment etc, so it's also replaced)
    String newFilename = Util.sanitizeFileName(filename);

    // Updating filename if it's changed after sanitizing.
    if (!newFilename.equals(filename)) {
        log.info("Filename changed from " + filename + " to " + newFilename);
        filename = newFilename;
    }

    try {
        ct = p.getContentType();
        if (filename.indexOf(".") < 0) // no ext in filename... let's fix it if possible
        {
            // Using startsWith instead of equals because sometimes the ct has crud beyond the image/jpeg;...crud....
            // Below are the most common file types, more type can be added if needed

            // Most common APPLICATION TYPE
            if (ct.startsWith("application/pdf"))
                filename = filename + ".pdf";
            if (ct.startsWith("application/zip"))
                filename = filename + ",zip";
            // Most common IMAGE TYPE
            if (ct.startsWith("image/jpeg"))
                filename = filename + ".jpg";
            if (ct.startsWith("image/gif"))
                filename = filename + ".gif";
            if (ct.startsWith("image/png"))
                filename = filename + ".png";
            // Most Common VIDEO TYPE
            if (ct.startsWith("video/x-ms-wmv"))
                filename = filename + ".wmv";
            // Most Common AUDIO TYPE
            if (ct.startsWith("audio/mpeg"))
                filename = filename + ".mp3";
            if (ct.startsWith("audio/mp4"))
                filename = filename + ".mp4";
            // Most Common TEXT TYPE
            if (ct.startsWith("text/html"))
                filename = filename + ".html";
            // Windows Office
            if (ct.startsWith("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) //Word
                filename = filename + ".docx";
            if (ct.startsWith("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) //Excel
                filename = filename + ".xlsx";
            if (ct.startsWith("application/vnd.openxmlformats-officedocument.presentationml.presentation")) //PowerPoint
                filename = filename + ".pptx";
        }
        // retain only up to first semi-colon; often ct is something like text/plain; name="filename"' we don't want to log the filename
        int x = ct.indexOf(";");
        if (x >= 0)
            ct = ct.substring(0, x);
        log.info("Attachment content type: " + ct + " filename = " + Util.blurKeepingExtension(filename));
    } catch (Exception pex) {
        dataErrors.add("Can't read CONTENT-TYPE: " + ct + " filename:" + filename + " size = " + p.getSize()
                + " subject: " + m.getSubject() + " Date : " + m.getSentDate().toString() + "\n Exception: "
                + pex + "\n" + Util.stackTrace(pex));
        return;
    }

    //       if (filename == null && !p.isMimeType("text/html") && !p.isMimeType("message/partial")) // expected not to have a filename with mime type text/html
    //          log.warn ("Attachment filename is null: " + Util.stackTrace());

    boolean success = true;
    // the size passed in here is the part size, which is not really the binary blob size.
    // when we read the stream below in blobStore.add(), we'll set it again to the binary blob size
    Blob b = new EmailAttachmentBlob(filename, p.getSize(), (MimeMessage) m, p);

    if (fetchConfig.downloadAttachments) {
        // this containment check is only on the basis of file name and size currently,
        // not on the actual hash
        if (archive.getBlobStore().contains(b)) {
            log.debug("Cache hit! " + b);
        } else {
            try {
                if (filename.endsWith(".tif"))
                    log.info("Fetching attachment..." + Util.blurKeepingExtension(filename));

                // performance critical! use large buffer! currently 256KB
                // stream will be closed by callee

                long start = System.currentTimeMillis();
                long nBytes = archive.getBlobStore().add(b,
                        new BufferedInputStream(p.getInputStream(), 256 * 1024));
                long end = System.currentTimeMillis();
                if (nBytes != -1) {
                    long diff = end - start;
                    String s = "attachment size " + nBytes + " bytes, fetched in " + diff + " millis";
                    if (diff > 0)
                        s += " (" + (nBytes / diff) + " KB/s)";
                    log.info(s);
                }

                Util.ASSERT(archive.getBlobStore().contains(b));

            } catch (IOException ioe) {
                success = false;
                dataErrors.add("WARNING: Unable to fetch attachment: filename: " + filename + " size = "
                        + p.getSize() + " subject: " + m.getSubject() + " Date : " + m.getSentDate().toString()
                        + "\nException: " + ioe);
                ioe.printStackTrace(System.out);
            }
        }

        if (success) {
            attachmentsList.add(b);

            /// generate thumbnail only if not already cached
            try {
                archive.getBlobStore().generate_thumbnail(b); // supplement
            } catch (IOException ioe) {
                log.warn("failed to create thumbnail, filename: " + filename + " size = " + p.getSize()
                        + " subject: " + m.getSubject() + " Date : " + m.getSentDate().toString()
                        + "\nException: " + ioe);
                ioe.printStackTrace(System.out);
            }
        }
    }
}

From source file:org.nuclos.server.ruleengine.RuleInterface.java

private void getAttachments(Multipart multipart, NuclosMail mail) throws MessagingException, IOException {
    for (int i = 0; i < multipart.getCount(); i++) {
        Part part = multipart.getBodyPart(i);
        String disposition = part.getDisposition();
        MimeBodyPart mimePart = (MimeBodyPart) part;
        logger.debug("Disposition: " + disposition + "; Part ContentType: " + mimePart.getContentType());

        if (part.getContent() instanceof Multipart) {
            logger.debug("Start child Multipart.");
            Multipart childmultipart = (Multipart) part.getContent();
            getAttachments(childmultipart, mail);
            logger.debug("Finished child Multipart.");
        } else if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
            logger.debug("Attachment: " + mimePart.getFileName());
            InputStream in = mimePart.getInputStream();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[8192];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);/*  w ww  .  j  a v  a 2  s  . c o m*/
            }
            mail.addAttachment(new NuclosFile(mimePart.getFileName(), out.toByteArray()));
        }
    }
}

From source file:org.xwiki.contrib.mail.internal.JavamailMessageParser.java

/**
 * {@inheritDoc}// ww  w  . j  a v  a2 s  .  co  m
 * 
 * @throws MessagingException
 * @throws IOException
 * @see org.xwiki.contrib.mail.internal.IMessageParser#parseHeaders(java.lang.Object)
 */
@Override
public MailItem parseHeaders(Part mail) throws MessagingException, IOException {
    MailItem m = new MailItem();

    String[] headers;

    String value = null;

    value = extractSingleHeader(mail, "Message-ID");
    value = Utils.cropId(value);
    m.setMessageId(value);

    value = extractSingleHeader(mail, "In-Reply-To");
    value = Utils.cropId(value);
    m.setReplyToId(value);

    value = extractSingleHeader(mail, "References");
    m.setRefs(value);

    value = extractSingleHeader(mail, "Subject");
    if (StringUtils.isBlank(value)) {
        value = DEFAULT_SUBJECT;
    }
    value = value.replaceAll("[\n\r]", "").replaceAll(">", "&gt;").replaceAll("<", "&lt;");
    m.setSubject(value);

    // If topic is not provided, we use message subject without the beginning junk
    value = extractSingleHeader(mail, "Thread-Topic");
    if (StringUtils.isBlank(value)) {
        value = m.getSubject().replaceAll("(?mi)([\\[\\(] *)?(RE|FWD?) *([-:;)\\]][ :;\\])-]*|$)|\\]+ *$", "");
    } else {
        value = Utils.removeCRLF(value);
    }
    m.setTopic(value);

    // Topic Id : if none is provided, we use the message-id as topic id
    value = extractSingleHeader(mail, "Thread-Index");
    if (!StringUtils.isBlank(value)) {
        value = Utils.cropId(value);
    }
    m.setTopicId(value);

    value = extractSingleHeader(mail, "From");
    value = value.replaceAll("\"", "").replaceAll("[\n\r]", "");
    m.setFrom(value);

    value = extractSingleHeader(mail, "Sender");
    value = value.replaceAll("\"", "").replaceAll("[\n\r]", "");
    m.setSender(value);

    value = extractSingleHeader(mail, "To");
    value = value.replaceAll("\"", "").replaceAll("[\n\r]", "");
    m.setTo(value);

    value = extractSingleHeader(mail, "CC");
    value = value.replaceAll("\"", "").replaceAll("[\n\r]", "");
    m.setCc(value);

    // process the locale, if any provided
    String locLang = "en";
    String locCountry = "US";
    String language;
    headers = mail.getHeader("Content-Language");
    if (headers != null) {
        language = headers[0];
        if (language != null && !language.isEmpty()) {
            int index = language.indexOf('.');
            if (index != -1) {
                locLang = language.substring(0, index - 1);
                locCountry = language.substring(index);
            }
        }
    }
    Locale locale = new Locale(locLang, locCountry);
    m.setLocale(locale);

    String date = "";
    Date decodedDate = null;
    headers = mail.getHeader("Date");
    if (headers != null) {
        date = headers[0];
    }
    // Decode the date
    try {
        logger.debug("Parsing date [" + date + "] with Javamail MailDateFormat");
        decodedDate = new MailDateFormat().parse(date);
    } catch (ParseException e) {
        logger.debug("Could not parse date header " + ExceptionUtils.getRootCauseMessage(e));
        decodedDate = null;
    }
    if (decodedDate == null) {
        try {
            logger.debug("Parsing date [" + date + "] with GMail parser");
            decodedDate = new GMailMailDateFormat().parse(date);
        } catch (ParseException e) {
            logger.info(
                    "Could not parse date header with GMail parser " + ExceptionUtils.getRootCauseMessage(e));
            decodedDate = new Date();
            logger.info("Using 'now' as date as date could not be parsed");
        }
    }
    m.setDate(decodedDate);

    boolean firstInTopic = ("".equals(m.getReplyToId()));
    m.setFirstInTopic(firstInTopic);

    m.setOriginalMessage((Message) mail);
    m.setBodypart(mail.getContent());
    m.setContentType(mail.getContentType().toLowerCase());

    String sensitivity = "normal";
    headers = mail.getHeader("Sensitivity");
    if (headers != null && !headers[0].isEmpty()) {
        sensitivity = "normal";
    }
    m.setSensitivity(sensitivity.toLowerCase());

    String importance = "normal";
    headers = mail.getHeader("Importance");
    if (importance == null || importance == "") {
        importance = "normal";
    }
    m.setImportance(importance.toLowerCase());

    // type
    m.setBuiltinType("mail");

    return m;
}