Example usage for javax.mail.internet MimePart setText

List of usage examples for javax.mail.internet MimePart setText

Introduction

In this page you can find the example usage for javax.mail.internet MimePart setText.

Prototype

public void setText(String text, String charset) throws MessagingException;

Source Link

Document

Convenience method that sets the given String as this part's content, with a MIME type of "text/plain" and the specified charset.

Usage

From source file:com.zimbra.cs.service.FeedManager.java

private static ParsedMessage generateMessage(String title, String content, String ctype, InternetAddress addr,
        Date date, List<Enclosure> attach) throws ServiceException {
    // cull out invalid enclosures
    if (attach != null) {
        for (Iterator<Enclosure> it = attach.iterator(); it.hasNext();) {
            if (it.next().getLocation() == null) {
                it.remove();// w  w  w  . j  a  va 2s  . c o m
            }
        }
    }
    boolean hasAttachments = attach != null && !attach.isEmpty();

    // clean up whitespace in the title
    if (title != null) {
        title = title.replaceAll("\\s+", " ");
    }

    // create the MIME message and wrap it
    try {
        MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
        MimePart body = hasAttachments ? new ZMimeBodyPart() : (MimePart) mm;
        body.setText(content, "utf-8");
        body.setHeader("Content-Type", ctype);

        if (hasAttachments) {
            // encode each enclosure as an attachment with Content-Location set
            MimeMultipart mmp = new ZMimeMultipart("mixed");
            mmp.addBodyPart((BodyPart) body);
            for (Enclosure enc : attach) {
                MimeBodyPart part = new ZMimeBodyPart();
                part.setText("");
                part.addHeader("Content-Location", enc.getLocation());
                part.addHeader("Content-Type", enc.getContentType());
                if (enc.getDescription() != null) {
                    part.addHeader("Content-Description", enc.getDescription());
                }
                part.addHeader("Content-Disposition", "attachment");
                mmp.addBodyPart(part);
            }
            mm.setContent(mmp);
        }

        mm.setSentDate(date);
        mm.addFrom(new InternetAddress[] { addr });
        mm.setSubject(title, "utf-8");
        // more stuff here!
        mm.saveChanges();
        return new ParsedMessage(mm, date.getTime(), false);
    } catch (MessagingException e) {
        throw ServiceException.PARSE_ERROR("error wrapping feed item in MimeMessage", e);
    }
}

From source file:com.zimbra.cs.mime.Mime.java

private static List<MPartInfo> listParts(MimePart root, String defaultCharset)
        throws MessagingException, IOException {
    List<MPartInfo> parts = new ArrayList<MPartInfo>();

    LinkedList<MPartInfo> queue = new LinkedList<MPartInfo>();
    queue.add(generateMPartInfo(root, null, "", 0));

    MimeMultipart emptyMultipart = null;
    while (!queue.isEmpty()) {
        MPartInfo mpart = queue.removeFirst();
        MimePart mp = mpart.getMimePart();
        parts.add(mpart);//from   www . j  a v  a  2  s .  c om

        String cts = mpart.mContentType;
        boolean isMultipart = cts.startsWith(MimeConstants.CT_MULTIPART_PREFIX);
        boolean isMessage = !isMultipart && cts.equals(MimeConstants.CT_MESSAGE_RFC822);

        if (isMultipart) {
            // IMAP part numbering is screwy: top-level multipart doesn't get a number
            String prefix = mpart.mPartName.length() > 0 ? (mpart.mPartName + '.') : "";
            if (mp instanceof MimeMessage) {
                mpart.mPartName = prefix + "TEXT";
            }
            MimeMultipart multi = getMultipartContent(mp, cts);
            if (multi != null) {
                if (multi.getCount() == 0 && LC.mime_promote_empty_multipart.booleanValue()) {
                    if (emptyMultipart == null) {
                        emptyMultipart = multi;
                    }
                    if (MimeConstants.CT_MULTIPART_APPLEDOUBLE.equalsIgnoreCase(getContentType(mp))) {
                        ZimbraLog.misc.debug(
                                "appledouble with no children; assuming it is malformed and really applefile");
                        mpart.mContentType = mpart.mContentType.replace(MimeConstants.CT_MULTIPART_APPLEDOUBLE,
                                MimeConstants.CT_APPLEFILE);
                    }
                }
                mpart.mChildren = new ArrayList<MPartInfo>(multi.getCount());
                for (int i = 1; i <= multi.getCount(); i++) {
                    mpart.mChildren
                            .add(generateMPartInfo((MimePart) multi.getBodyPart(i - 1), mpart, prefix + i, i));
                }
                queue.addAll(0, mpart.mChildren);
            }
        } else if (isMessage) {
            MimeMessage mm = getMessageContent(mp);
            if (mm != null) {
                MPartInfo child = generateMPartInfo(mm, mpart, mpart.mPartName, 0);
                queue.addFirst(child);
                mpart.mChildren = Arrays.asList(child);
            }
        } else {
            // nothing to do at this stage
        }
    }

    if (emptyMultipart != null && parts.size() == 1) {
        String text = emptyMultipart.getPreamble();
        if (!StringUtil.isNullOrEmpty(text)) {
            ZimbraLog.misc
                    .debug("single multipart with no children. promoting the preamble into a single text part");
            parts.remove(0);
            MPartInfo mpart = new MPartInfo();
            ZMimeBodyPart mp = new ZMimeBodyPart();
            mp.setText(text, defaultCharset);
            mpart.mPart = mp;
            mpart.mContentType = mp.getContentType();
            mpart.mDisposition = "";
            mpart.mPartName = "1";
            parts.add(mpart);
        }
    }

    return parts;
}