Example usage for javax.mail Multipart getCount

List of usage examples for javax.mail Multipart getCount

Introduction

In this page you can find the example usage for javax.mail Multipart getCount.

Prototype

public synchronized int getCount() throws MessagingException 

Source Link

Document

Return the number of enclosed BodyPart objects.

Usage

From source file:com.sonicle.webtop.mail.Service.java

private boolean appendReplyParts(Part p, StringBuffer htmlsb, StringBuffer textsb, ArrayList<String> attnames)
        throws MessagingException, IOException {
    boolean isHtml = false;
    String disp = p.getDisposition();
    if (disp != null && disp.equalsIgnoreCase(Part.ATTACHMENT) && !p.isMimeType("message/*")) {
        if (attnames != null) {/*  w w  w.  j  a v  a 2s  .  c o  m*/
            String id[] = p.getHeader("Content-ID");
            if (id == null || id[0] == null) {
                String filename = p.getFileName();
                if (filename != null) {
                    if (filename.startsWith("<")) {
                        filename = filename.substring(1);
                    }
                    if (filename.endsWith(">")) {
                        filename = filename.substring(0, filename.length() - 1);
                    }
                }
                if (filename != null) {
                    attnames.add(filename);
                }
            }
        }
        return false;
    }
    if (p.isMimeType("text/html")) {
        //String htmlcontent=(String)p.getContent();
        String htmlcontent = getTextContentAsString(p);
        textsb.append(MailUtils.htmlToText(MailUtils.htmlunescapesource(htmlcontent)));
        htmlsb.append(MailUtils.htmlescapefixsource(/*getBodyInnerHtml(*/htmlcontent/*)*/));
        isHtml = true;
    } else if (p.isMimeType("text/plain")) {
        String content = getTextContentAsString(p);
        textsb.append(content);
        htmlsb.append(startpre + MailUtils.htmlescape(content) + endpre);
        isHtml = false;
    } else if (p.isMimeType("message/delivery-status") || p.isMimeType("message/disposition-notification")) {
        InputStream is = (InputStream) p.getContent();
        char cbuf[] = new char[8000];
        byte bbuf[] = new byte[8000];
        int n = 0;
        htmlsb.append(startpre);
        while ((n = is.read(bbuf)) >= 0) {
            if (n > 0) {
                for (int i = 0; i < n; ++i) {
                    cbuf[i] = (char) bbuf[i];
                }
                textsb.append(cbuf);
                htmlsb.append(MailUtils.htmlescape(new String(cbuf)));
            }
        }
        htmlsb.append(endpre);
        is.close();
        isHtml = false;
    } else if (p.isMimeType("multipart/alternative")) {
        Multipart mp = (Multipart) p.getContent();
        Part bestPart = null;
        for (int i = 0; i < mp.getCount(); ++i) {
            Part part = mp.getBodyPart(i);
            if (part.isMimeType("multipart/*")) {
                isHtml = appendReplyParts(part, htmlsb, textsb, attnames);
                if (isHtml) {
                    bestPart = null;
                    break;
                }
            } else if (part.isMimeType("text/html")) {
                bestPart = part;
                break;
            } else if (bestPart == null && part.isMimeType("text/plain")) {
                bestPart = part;
            } else if (bestPart == null && part.isMimeType("message/*")) {
                bestPart = part;
            }
        }
        if (bestPart != null) {
            isHtml = appendReplyParts(bestPart, htmlsb, textsb, attnames);
        }
    } else if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        for (int i = 0; i < mp.getCount(); ++i) {
            if (appendReplyParts(mp.getBodyPart(i), htmlsb, textsb, attnames)) {
                isHtml = true;
            }
        }
    } else if (p.isMimeType("message/*")) {
        Object content = p.getContent();
        if (appendReplyParts((MimeMessage) content, htmlsb, textsb, attnames)) {
            isHtml = true;
        }
    } else {
    }
    textsb.append('\n');
    textsb.append('\n');

    return isHtml;
}