Example usage for javax.mail BodyPart isMimeType

List of usage examples for javax.mail BodyPart isMimeType

Introduction

In this page you can find the example usage for javax.mail BodyPart isMimeType.

Prototype

public boolean isMimeType(String mimeType) throws MessagingException;

Source Link

Document

Is this Part of the specified MIME type?

Usage

From source file:Main.java

public static boolean isContainAttachment(Part part) throws MessagingException, IOException {
    boolean flag = false;
    if (part.isMimeType("multipart/*")) {
        MimeMultipart multipart = (MimeMultipart) part.getContent();
        int partCount = multipart.getCount();
        for (int i = 0; i < partCount; i++) {
            BodyPart bodyPart = multipart.getBodyPart(i);
            String disp = bodyPart.getDisposition();
            if (disp != null
                    && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
                flag = true;//from   w  w w  . j av a 2  s  . co  m
            } else if (bodyPart.isMimeType("multipart/*")) {
                flag = isContainAttachment(bodyPart);
            } else {
                String contentType = bodyPart.getContentType();
                if (contentType.indexOf("application") != -1) {
                    flag = true;
                }

                if (contentType.indexOf("name") != -1) {
                    flag = true;
                }
            }

            if (flag)
                break;
        }
    } else if (part.isMimeType("message/rfc822")) {
        flag = isContainAttachment((Part) part.getContent());
    }
    return flag;
}

From source file:mitm.common.mail.BodyPartUtils.java

/**
 * Searches for an embedded RFC822 messages. Returns the first embedded RFC822 it finds.
 * Only one level deep is searched.// w w w . ja va 2  s  .co  m
 *  
 * Returns null if there are no embedded message.
 */
public static MimeMessage searchForRFC822(MimeMessage message) throws MessagingException, IOException {
    /*
     * Fast fail. Only multipart mixed messages are supported. 
     */
    if (!message.isMimeType("multipart/mixed")) {
        return null;
    }

    Multipart mp;

    try {
        mp = (Multipart) message.getContent();
    } catch (IOException e) {
        throw new MessagingException("Error getting message content.", e);
    }

    MimeMessage embeddedMessage = null;

    for (int i = 0; i < mp.getCount(); i++) {
        BodyPart part = mp.getBodyPart(i);

        if (part.isMimeType("message/rfc822")) {
            embeddedMessage = BodyPartUtils.extractFromRFC822(part);

            break;
        }
    }

    return embeddedMessage;
}

From source file:it.greenvulcano.gvesb.virtual.utils.MimeMessageHelper.java

public static Body getMessageBody(MimeMessage message, String mimeType) {
    try {/*from  www . j av  a  2 s. c o m*/

        if (message.getContent() instanceof Multipart) {

            Multipart multipartMessage = (Multipart) message.getContent();

            for (int i = 0; i < multipartMessage.getCount(); i++) {
                BodyPart bodyPart = multipartMessage.getBodyPart(i);

                if (bodyPart.isMimeType(mimeType) && (Part.INLINE.equalsIgnoreCase(bodyPart.getDisposition())
                        || Objects.isNull(bodyPart.getDisposition()))) {

                    return new Body(bodyPart.getContentType(), bodyPart.getContent().toString());

                }
            }
        } else {

            return new Body(message.getContentType(), message.getContent().toString());
        }

    } catch (Exception e) {
        // do nothing
    }

    return null;
}

From source file:com.zimbra.cs.util.SpamExtract.java

private static void writeAttachedMessages(MimeMessage mm, File outdir, String msgUri)
        throws IOException, MessagingException {
    // Not raw - ignore the spam report and extract messages that are in attachments...
    if (!(mm.getContent() instanceof MimeMultipart)) {
        LOG.warn("Spam/notspam messages must have attachments (skipping " + msgUri + ")");
        return;/*from  w ww  .j a  va 2s  .  c om*/
    }

    MimeMultipart mmp = (MimeMultipart) mm.getContent();
    int nAttachments = mmp.getCount();
    boolean foundAtleastOneAttachedMessage = false;
    for (int i = 0; i < nAttachments; i++) {
        BodyPart bp = mmp.getBodyPart(i);
        if (!bp.isMimeType("message/rfc822")) {
            // Let's ignore all parts that are not messages.
            continue;
        }
        foundAtleastOneAttachedMessage = true;
        Part msg = (Part) bp.getContent(); // the actual message
        File file = new File(outdir, mOutputPrefix + "-" + mExtractIndex++);
        OutputStream os = null;
        try {
            os = new BufferedOutputStream(new FileOutputStream(file));
            if (msg instanceof MimeMessage) {
                //bug 74435 clone into newMsg so our parser has a chance to handle headers which choke javamail
                ZMimeMessage newMsg = new ZMimeMessage((MimeMessage) msg);
                newMsg.writeTo(os);
            } else {
                msg.writeTo(os);
            }
        } finally {
            os.close();
        }
        if (verbose)
            LOG.info("Wrote: " + file);
    }

    if (!foundAtleastOneAttachedMessage) {
        String msgid = mm.getHeader("Message-ID", " ");
        LOG.warn("message uri=" + msgUri + " message-id=" + msgid + " had no attachments");
    }
}

From source file:mailbox.CreationViaEmail.java

private static int getPoint(BodyPart p, String[] preferences) throws MessagingException {
    if (p == null) {
        return 0;
    }/*from  ww  w .  j  av a  2 s.  co  m*/

    for (int i = 0; i < preferences.length; i++) {
        if (p.isMimeType(preferences[i])) {
            return preferences.length + 1 - i;
        }
    }

    return 1;
}

From source file:mitm.common.security.smime.SMIMEUtils.java

/**
 * Split up the multipart in a message part and signed part. Returns null if the message
 * does not contain one of these parts or if it contains more or less than 2 parts.
 * @param multiPart// w  w w .  j av  a 2 s .  co  m
 * @return first item is the message part, second the signature part
 * @throws MessagingException
 */
public static BodyPart[] dissectSigned(Multipart multipart) throws MessagingException {
    BodyPart messagePart = null;
    BodyPart signaturePart = null;

    if (multipart == null) {
        return null;
    }

    if (multipart.getCount() != 2) {
        logger.debug("Multipart does not contain 2 parts.");

        return null;
    }

    for (int i = 0; i < 2; i++) {
        BodyPart part = multipart.getBodyPart(i);

        /*
         * Check if we have found the signature part. We need to make sure that the content-type
         * is not multipart/signed because that fails when we have a clear signed message that
         * is signed again.
         */
        if (SMIMEHeader.getSMIMEContentType(part) == SMIMEHeader.Type.CLEAR_SIGNED
                && !part.isMimeType("multipart/signed")) {
            signaturePart = part;
        } else {
            messagePart = part;
        }
    }

    if (messagePart == null || signaturePart == null) {
        logger.debug("Multipart does not contain a message and signature part.");

        return null;
    }

    return new BodyPart[] { messagePart, signaturePart };
}

From source file:com.adaptris.core.services.mime.FlattenMimeParts.java

private List<BodyPart> extract(Multipart m) throws Exception {
    List<BodyPart> parts = new ArrayList<>();
    for (int i = 0; i < m.getCount(); i++) {
        BodyPart p = m.getBodyPart(i);
        if (p.isMimeType("multipart/*")) {
            parts.addAll(extract((Multipart) p.getContent()));
        } else {/*from ww  w. ja v  a  2 s  .  c om*/
            parts.add(p);
        }
    }
    return parts;
}

From source file:com.caris.oscarexchange4j.proxy.PreviewCoverage.java

@Override
public void processInputStream(InputStream is) throws Exception {
    try {//ww  w .j  a  va  2  s.  c  om
        MimeMultipart multiPart = new MimeMultipart(new ByteArrayDataSource(is, MULTI_PART_MIME_TYPE));
        int count = multiPart.getCount();
        for (int part = 0; part < count; part++) {
            BodyPart body = multiPart.getBodyPart(part);
            if (body.isMimeType(OUTPUT_MIME_TYPE)) {
                this.inputStream = body.getInputStream();
                break;
            }
        }
    } catch (Exception e) {
        this.inputStream = getErrorResponseStream();
    }
}

From source file:mitm.application.djigzo.ca.PFXMailBuilderTest.java

private byte[] getPFX(MimeMessage message) throws Exception {
    Multipart mp;//w w w  .  j a v a 2s.c om

    mp = (Multipart) message.getContent();

    BodyPart pdfPart = null;

    for (int i = 0; i < mp.getCount(); i++) {
        BodyPart part = mp.getBodyPart(i);

        if (part.isMimeType("application/octet-stream")) {
            pdfPart = part;
        }
    }

    assertNotNull(pdfPart);

    Object content = pdfPart.getContent();

    assertTrue(content instanceof ByteArrayInputStream);

    return IOUtils.toByteArray((ByteArrayInputStream) content);
}

From source file:mitm.application.djigzo.ca.PFXMailBuilderTest.java

@Test
public void testReplacePFXSendSMSFalse() throws Exception {
    byte[] pfx = IOUtils.toByteArray(new FileInputStream(testPFX));

    PFXMailBuilder builder = new PFXMailBuilder(IOUtils.toString(new FileInputStream(templateFile)),
            templateBuilder);//from   w  w w  . j a va 2 s .com

    String from = "123@test.com";

    builder.setFrom(new InternetAddress(from, "test user"));
    builder.setPFX(pfx);
    builder.addProperty("sendSMS", false);

    MimeMessage message = builder.createMessage();

    assertNotNull(message);

    MailUtils.writeMessage(message, new File(tempDir, "testReplacePFXSendSMSFalse.eml"));

    Multipart mp;

    mp = (Multipart) message.getContent();

    BodyPart textPart = mp.getBodyPart(0);

    assertTrue(textPart.isMimeType("text/plain"));

    String body = (String) textPart.getContent();

    assertFalse(body.contains("was sent to you by SMS"));

    /*
     * Check if the PFX has really been replaced
     */
    byte[] newPFX = getPFX(message);

    KeyStore keyStore = SecurityFactoryFactory.getSecurityFactory().createKeyStore("PKCS12");

    keyStore.load(new ByteArrayInputStream(newPFX), "test".toCharArray());

    assertEquals(22, keyStore.size());
}