Search in sources :

Example 56 with BodyPart

use of javax.mail.BodyPart in project camel by apache.

the class MimeMessageConsumeTest method populateMimeMessageBody.

/**
     * Lets encode a multipart mime message
     */
protected void populateMimeMessageBody(MimeMessage message) throws MessagingException {
    MimeBodyPart plainPart = new MimeBodyPart();
    plainPart.setText(body);
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setText("<html><body>" + body + "</body></html>");
    Multipart alt = new MimeMultipart("alternative");
    alt.addBodyPart(plainPart);
    alt.addBodyPart(htmlPart);
    Multipart mixed = new MimeMultipart("mixed");
    MimeBodyPart wrap = new MimeBodyPart();
    wrap.setContent(alt);
    mixed.addBodyPart(wrap);
    mixed.addBodyPart(plainPart);
    mixed.addBodyPart(htmlPart);
    DataSource ds;
    try {
        File f = new File(getClass().getResource("/log4j2.properties").toURI());
        ds = new FileDataSource(f);
    } catch (URISyntaxException ex) {
        ds = new URLDataSource(getClass().getResource("/log4j2.properties"));
    }
    DataHandler dh = new DataHandler(ds);
    BodyPart attachmentBodyPart;
    // Create another body part
    attachmentBodyPart = new MimeBodyPart();
    // Set the data handler to the attachment
    attachmentBodyPart.setDataHandler(dh);
    // Set the filename
    attachmentBodyPart.setFileName(dh.getName());
    // Set Disposition
    attachmentBodyPart.setDisposition(Part.ATTACHMENT);
    mixed.addBodyPart(plainPart);
    mixed.addBodyPart(htmlPart);
    // Add attachmentBodyPart to multipart
    mixed.addBodyPart(attachmentBodyPart);
    message.setContent(mixed);
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) URLDataSource(javax.activation.URLDataSource) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) URISyntaxException(java.net.URISyntaxException) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) FileDataSource(javax.activation.FileDataSource) URLDataSource(javax.activation.URLDataSource) DataSource(javax.activation.DataSource)

Example 57 with BodyPart

use of javax.mail.BodyPart in project zm-mailbox by Zimbra.

the class Mime method getMimePart.

public static MimePart getMimePart(MimePart mp, String part) throws IOException, MessagingException {
    if (mp == null) {
        return null;
    }
    if (part == null || part.trim().isEmpty()) {
        return mp;
    }
    part = part.trim();
    boolean digestParent = false;
    String[] subpart = part.split("\\.");
    for (int i = 0; i < subpart.length; i++) {
        int index = Integer.parseInt(subpart[i]);
        if (index <= 0) {
            return null;
        }
        // the content-type determines the expected substructure
        String ct = getContentType(mp, digestParent ? MimeConstants.CT_MESSAGE_RFC822 : MimeConstants.CT_DEFAULT);
        if (ct == null) {
            return null;
        }
        digestParent = ct.equals(MimeConstants.CT_MULTIPART_DIGEST);
        if (ct.startsWith(MimeConstants.CT_MULTIPART_PREFIX)) {
            MimeMultipart mmp = getMultipartContent(mp, ct);
            if (mmp != null && mmp.getCount() >= index) {
                BodyPart bp = mmp.getBodyPart(index - 1);
                if (bp instanceof MimePart) {
                    mp = (MimePart) bp;
                    continue;
                }
            }
        } else if (ct.equals(MimeConstants.CT_MESSAGE_RFC822) || (ct.equals(MimeConstants.CT_APPLICATION_OCTET_STREAM) && isEmlAttachment(mp))) {
            MimeMessage content = getMessageContent(mp);
            if (content != null) {
                if (mp instanceof MimeMessage) {
                    // the top-level part of a non-multipart message is numbered "1"
                    if (index != 1) {
                        return null;
                    }
                } else {
                    i--;
                }
                mp = content;
                continue;
            }
        } else if (mp instanceof MimeMessage && index == 1 && i == subpart.length - 1) {
            // the top-level part of a non-multipart message is numbered "1"
            break;
        }
        return null;
    }
    return mp;
}
Also used : ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MimePart(javax.mail.internet.MimePart) ZMimePart(com.zimbra.common.zmime.ZMimePart)

Example 58 with BodyPart

use of javax.mail.BodyPart in project zm-mailbox by Zimbra.

the class ZMimeParserTest method traverseChildren.

private void traverseChildren(ZMimeMultipart mp, int targetDepth) throws MessagingException, IOException {
    //traverse MIME tree and make sure the expected bottom item has no children
    if (targetDepth == 0) {
        Assert.assertTrue("depth at 0", mp.getCount() == 0);
        return;
    }
    BodyPart bp = mp.getBodyPart(1);
    Assert.assertTrue("not multipart?", bp instanceof ZMimeBodyPart);
    ZMimeBodyPart zbp = (ZMimeBodyPart) bp;
    Object content = zbp.getContent();
    Assert.assertTrue("not multipart?", content instanceof ZMimeMultipart);
    ZMimeMultipart zmp = (ZMimeMultipart) content;
    traverseChildren(zmp, targetDepth - 1);
}
Also used : BodyPart(javax.mail.BodyPart)

Example 59 with BodyPart

use of javax.mail.BodyPart in project zm-mailbox by Zimbra.

the class TnefConverter method visitMultipart.

@Override
protected boolean visitMultipart(MimeMultipart mmp, VisitPhase visitKind) throws MessagingException {
    // do the decode in the exit phase
    if (visitKind != VisitPhase.VISIT_END)
        return false;
    // proactively ignore already-converted TNEF attachments
    if (MimeConstants.CT_MULTIPART_ALTERNATIVE.equals(mmp.getContentType()))
        return false;
    try {
        boolean found = false;
        for (int i = 0; i < mmp.getCount() && !found; i++) {
            BodyPart bp = mmp.getBodyPart(i);
            found = bp instanceof MimeBodyPart && TNEFUtils.isTNEFMimeType(bp.getContentType());
        }
        if (!found)
            return false;
        // check to make sure that the caller's OK with altering the message
        if (mCallback != null && !mCallback.onModification())
            return false;
    } catch (MessagingException e) {
        ZimbraLog.extensions.warn("exception while traversing multipart; skipping", e);
        return false;
    }
    List<MultipartReplacement> tnefReplacements = new ArrayList<MultipartReplacement>();
    try {
        for (int i = 0; i < mmp.getCount(); i++) {
            BodyPart bp = mmp.getBodyPart(i);
            if (bp instanceof MimeBodyPart && TNEFUtils.isTNEFMimeType(bp.getContentType())) {
                // try to expand the TNEF into a suitable Multipart
                MimeMultipart multi = null;
                try {
                    multi = expandTNEF((MimeBodyPart) bp);
                } catch (Exception e) {
                    ZimbraLog.extensions.warn("exception while decoding TNEF; skipping part", e);
                    continue;
                }
                if (multi == null)
                    continue;
                tnefReplacements.add(new MultipartReplacement(i, (MimeBodyPart) bp, multi));
            }
        }
    } catch (MessagingException e) {
        ZimbraLog.extensions.warn("exception while traversing multipart; skipping", e);
        return false;
    }
    for (MultipartReplacement change : tnefReplacements) {
        // unhitch the old TNEF part from the parent multipart and add it to the new converted one
        mmp.removeBodyPart(change.partIndex);
        change.converted.addBodyPart(change.tnefPart, 0);
        // create a BodyPart to contain the new Multipart (JavaMail bookkeeping)
        MimeBodyPart replacementPart = new ZMimeBodyPart();
        replacementPart.setContent(change.converted);
        // and put the new multipart/alternatives where the TNEF used to be
        mmp.addBodyPart(replacementPart, change.partIndex);
    }
    return !tnefReplacements.isEmpty();
}
Also used : ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MessagingException(javax.mail.MessagingException) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) ArrayList(java.util.ArrayList) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException)

Example 60 with BodyPart

use of javax.mail.BodyPart in project zm-mailbox by Zimbra.

the class TnefConverter method expandTNEF.

/**
     * Performs the TNEF->MIME conversion on any TNEF body parts that
     * make up the given message.
     * @throws ServiceException
     */
private MimeMultipart expandTNEF(MimeBodyPart bp) throws MessagingException, IOException {
    if (!TNEFUtils.isTNEFMimeType(bp.getContentType()))
        return null;
    MimeMessage converted = null;
    // convert TNEF to a MimeMessage and remove it from the parent
    InputStream is = null;
    try {
        TNEFInputStream tnefis = new TNEFInputStream(is = bp.getInputStream());
        converted = TNEFMime.convert(JMSession.getSession(), tnefis);
    // XXX bburtin: nasty hack.  Don't handle OOME since JTNEF can allocate a huge byte
    // array when the TNEF file is malformed.  See bug 42649.
    // } catch (OutOfMemoryError e) {
    //    Zimbra.halt("Ran out of memory while expanding TNEF attachment", e);
    } catch (Throwable t) {
        ZimbraLog.extensions.warn("Conversion failed.  TNEF attachment will not be expanded.", t);
        return null;
    } finally {
        ByteUtil.closeStream(is);
    }
    Object convertedContent = converted.getContent();
    if (!(convertedContent instanceof MimeMultipart)) {
        ZimbraLog.extensions.debug("TNEF attachment doesn't contain valid MimeMultiPart");
        return null;
    }
    MimeMultipart convertedMulti = (MimeMultipart) convertedContent;
    // make sure that all the attachments are marked as attachments
    for (int i = 0; i < convertedMulti.getCount(); i++) {
        BodyPart subpart = convertedMulti.getBodyPart(i);
        if (subpart.getHeader("Content-Disposition") == null)
            subpart.setHeader("Content-Disposition", Part.ATTACHMENT);
    }
    // Create a MimeBodyPart for the converted data.  Currently we're throwing
    // away the top-level message because its content shows up as blank after
    // the conversion.
    MimeBodyPart convertedPart = new ZMimeBodyPart();
    convertedPart.setContent(convertedMulti);
    // If the TNEF object contains calendar data, create an iCalendar version.
    MimeBodyPart icalPart = null;
    if (DebugConfig.enableTnefToICalendarConversion) {
        try {
            TnefToICalendar calConverter = new DefaultTnefToICalendar();
            ZCalendar.DefaultContentHandler icalHandler = new ZCalendar.DefaultContentHandler();
            if (calConverter.convert(mMimeMessage, bp.getInputStream(), icalHandler)) {
                if (icalHandler.getNumCals() > 0) {
                    List<ZVCalendar> cals = icalHandler.getCals();
                    Writer writer = new StringWriter(1024);
                    ICalTok method = null;
                    for (ZVCalendar cal : cals) {
                        cal.toICalendar(writer);
                        if (method == null)
                            method = cal.getMethod();
                    }
                    writer.close();
                    icalPart = new ZMimeBodyPart();
                    icalPart.setText(writer.toString());
                    ContentType ct = new ContentType(MimeConstants.CT_TEXT_CALENDAR);
                    ct.setCharset(MimeConstants.P_CHARSET_UTF8);
                    if (method != null)
                        ct.setParameter("method", method.toString());
                    icalPart.setHeader("Content-Type", ct.toString());
                }
            }
        } catch (ServiceException e) {
            throw new MessagingException("TNEF to iCalendar conversion failure: " + e.getMessage(), e);
        } catch (Throwable t) {
            //don't allow TNEF errors to crash server
            ZimbraLog.extensions.warn("Failed to convert TNEF to iCal", t);
            throw new MessagingException("TNEF to iCalendar conversion failure");
        }
    }
    // create a multipart/alternative for the TNEF and its MIME version
    MimeMultipart altMulti = new ZMimeMultipart("alternative");
    //        altMulti.addBodyPart(bp);
    altMulti.addBodyPart(convertedPart);
    if (icalPart != null)
        altMulti.addBodyPart(icalPart);
    return altMulti;
}
Also used : ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) ContentType(com.zimbra.common.mime.ContentType) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) TNEFInputStream(net.freeutils.tnef.TNEFInputStream) InputStream(java.io.InputStream) TNEFInputStream(net.freeutils.tnef.TNEFInputStream) DefaultTnefToICalendar(com.zimbra.cs.util.tnef.DefaultTnefToICalendar) ICalTok(com.zimbra.common.calendar.ZCalendar.ICalTok) ZCalendar(com.zimbra.common.calendar.ZCalendar) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) StringWriter(java.io.StringWriter) ServiceException(com.zimbra.common.service.ServiceException) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) TnefToICalendar(com.zimbra.cs.util.tnef.TnefToICalendar) DefaultTnefToICalendar(com.zimbra.cs.util.tnef.DefaultTnefToICalendar) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

BodyPart (javax.mail.BodyPart)77 MimeMultipart (javax.mail.internet.MimeMultipart)57 MimeBodyPart (javax.mail.internet.MimeBodyPart)46 MessagingException (javax.mail.MessagingException)23 MimeMessage (javax.mail.internet.MimeMessage)21 Multipart (javax.mail.Multipart)16 Header (javax.mail.Header)15 IOException (java.io.IOException)14 DispositionNotification (com.sun.mail.dsn.DispositionNotification)13 HashMap (java.util.HashMap)13 DataHandler (javax.activation.DataHandler)13 InternetHeaders (javax.mail.internet.InternetHeaders)13 ByteString (com.linkedin.data.ByteString)12 Test (org.testng.annotations.Test)10 FileDataSource (javax.activation.FileDataSource)9 Session (javax.mail.Session)9 InternetAddress (javax.mail.internet.InternetAddress)8 Properties (java.util.Properties)7 File (java.io.File)6 InputStream (java.io.InputStream)6