Example usage for javax.mail.internet MimeMultipart getBodyPart

List of usage examples for javax.mail.internet MimeMultipart getBodyPart

Introduction

In this page you can find the example usage for javax.mail.internet MimeMultipart getBodyPart.

Prototype

public synchronized BodyPart getBodyPart(String CID) throws MessagingException 

Source Link

Document

Get the MimeBodyPart referred to by the given ContentID (CID).

Usage

From source file:it.greenvulcano.gvesb.virtual.http.HTTPCallOperation.java

/**
 * @param responseBody// w  w  w . j  a va  2  s.c  o  m
 * @throws MessagingException
 */
private Document handleMultipart(byte[] responseBody, String contentType) throws Exception {
    ByteArrayDataSource bads = new ByteArrayDataSource(responseBody, contentType);
    MimeMultipart multipart = new MimeMultipart(bads);
    XMLUtils xml = XMLUtils.getParserInstance();
    Document doc = null;
    try {
        doc = xml.newDocument("MultipartHttpResponse");
    } finally {
        XMLUtils.releaseParserInstance(xml);
    }
    for (int i = 0; i < multipart.getCount(); i++) {
        dumpPart(multipart.getBodyPart(i), doc.getDocumentElement(), doc);
    }
    return doc;
}

From source file:com.haulmont.cuba.core.app.EmailerTest.java

private MimeBodyPart getTextPart(MimeMessage msg) throws IOException, MessagingException {
    assertTrue(msg.getContent() instanceof MimeMultipart);
    MimeMultipart mimeMultipart = (MimeMultipart) msg.getContent();

    Object content2 = mimeMultipart.getBodyPart(0).getContent();
    assertTrue(content2 instanceof MimeMultipart);
    MimeMultipart textBodyPart = (MimeMultipart) content2;

    return (MimeBodyPart) textBodyPart.getBodyPart(0);
}

From source file:com.haulmont.cuba.core.app.EmailerTest.java

private MimeBodyPart getInlineAttachment(MimeMessage msg) throws IOException, MessagingException {
    assertTrue(msg.getContent() instanceof MimeMultipart);
    MimeMultipart mimeMultipart = (MimeMultipart) msg.getContent();

    Object content2 = mimeMultipart.getBodyPart(0).getContent();
    assertTrue(content2 instanceof MimeMultipart);
    MimeMultipart textBodyPart = (MimeMultipart) content2;

    return (MimeBodyPart) textBodyPart.getBodyPart(1);
}

From source file:org.apache.james.transport.mailets.managesieve.ManageSieveMailetTestCase.java

private void ensureResponse(String subject, String... contents) throws MessagingException, IOException {
    MimeMessage result = verifyHeaders(subject);
    MimeMultipart multipart = (MimeMultipart) result.getContent();
    assertThat(multipart.getCount()).isEqualTo(contents.length);
    for (int i = 0; i < contents.length; i++) {
        if (multipart.getBodyPart(i).getContent() instanceof String) {
            assertThat(((String) multipart.getBodyPart(i).getContent()).trim()).isEqualTo(contents[i]);
        } else {//  ww  w  .j  a  va2  s.c o  m
            assertThat(IOUtils.toString((ByteArrayInputStream) multipart.getBodyPart(i).getContent()).trim())
                    .isEqualTo(contents[i]);
        }
    }
}

From source file:mitm.application.djigzo.james.mailets.BlackberrySMIMEAdapterTest.java

@Test
public void testInvalidEncoding() throws Exception {
    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    SendMailEventListenerImpl listener = new SendMailEventListenerImpl();

    mailetConfig.getMailetContext().setSendMailEventListener(listener);

    Mailet mailet = new BlackberrySMIMEAdapter();

    String template = FileUtils.readFileToString(
            new File("test/resources/templates/blackberry-smime-adapter-invalid-encoding.ftl"));

    autoTransactDelegator.setProperty("test@EXAMPLE.com", "pdfTemplate", template);

    mailetConfig.setInitParameter("log", "starting");
    /* use some dummy template because we must specify the default template */
    mailetConfig.setInitParameter("template", "sms.ftl");
    mailetConfig.setInitParameter("templateProperty", "pdfTemplate");

    mailet.init(mailetConfig);//  w w w .  jav a2  s .  c o m

    MockMail mail = new MockMail();

    mail.setState(Mail.DEFAULT);

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/signed-opaque-validcertificate.eml"));

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("123@EXAMPLE.com"));

    mail.setRecipients(recipients);

    mail.setSender(new MailAddress("sender@example.com"));

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    assertTrue(mail.getMessage().isMimeType("multipart/mixed"));
    assertFalse(mail.getMessage() == message);

    MimeMultipart mp = (MimeMultipart) mail.getMessage().getContent();

    assertEquals(2, mp.getCount());

    BodyPart bp = mp.getBodyPart(0);

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

    bp = mp.getBodyPart(1);

    assertTrue(bp.isMimeType("application/octet-stream"));
    assertEquals("x-rimdevicesmime.p7m", bp.getFileName());
}

From source file:mitm.application.djigzo.james.mailets.BlackberrySMIMEAdapterTest.java

@Test
public void testSMIMEAdaptMessage() throws Exception {
    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    SendMailEventListenerImpl listener = new SendMailEventListenerImpl();

    mailetConfig.getMailetContext().setSendMailEventListener(listener);

    Mailet mailet = new BlackberrySMIMEAdapter();

    String template = FileUtils.readFileToString(new File("resources/templates/blackberry-smime-adapter.ftl"));

    autoTransactDelegator.setProperty("test@EXAMPLE.com", "pdfTemplate", template);

    mailetConfig.setInitParameter("log", "starting");
    /* use some dummy template because we must specify the default template */
    mailetConfig.setInitParameter("template", "sms.ftl");
    mailetConfig.setInitParameter("templateProperty", "pdfTemplate");

    mailet.init(mailetConfig);/*from w  ww .ja  v  a  2  s .co m*/

    MockMail mail = new MockMail();

    mail.setState(Mail.DEFAULT);

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/signed-opaque-validcertificate.eml"));

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("123@EXAMPLE.com"));

    mail.setRecipients(recipients);

    mail.setSender(new MailAddress("sender@example.com"));

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    TestUtils.saveMessages(tempDir, "testSMIMEAdaptMessage", mail.getMessage());

    assertTrue(mail.getMessage().isMimeType("multipart/mixed"));
    assertFalse(mail.getMessage() == message);

    MimeMultipart mp = (MimeMultipart) mail.getMessage().getContent();

    assertEquals(2, mp.getCount());

    BodyPart bp = mp.getBodyPart(0);

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

    bp = mp.getBodyPart(1);

    assertTrue(bp.isMimeType("application/octet-stream"));
    assertEquals("x-rimdevicesmime.p7m", bp.getFileName());
}

From source file:mitm.application.djigzo.james.mailets.BlackberrySMIMEAdapterTest.java

@Test
public void testSMIMEAdaptMessageOverDirectLimit() throws Exception {
    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    SendMailEventListenerImpl listener = new SendMailEventListenerImpl();

    mailetConfig.getMailetContext().setSendMailEventListener(listener);

    Mailet mailet = new BlackberrySMIMEAdapter();

    String template = FileUtils.readFileToString(new File("resources/templates/blackberry-smime-adapter.ftl"));

    autoTransactDelegator.setProperty("test@EXAMPLE.com", "pdfTemplate", template);

    mailetConfig.setInitParameter("log", "starting");
    /* use some dummy template because we must specify the default template */
    mailetConfig.setInitParameter("template", "sms.ftl");
    mailetConfig.setInitParameter("templateProperty", "pdfTemplate");
    mailetConfig.setInitParameter("directSizeLimit", "100");

    mailet.init(mailetConfig);// w  w  w  . j ava2  s.  c  o  m

    MockMail mail = new MockMail();

    mail.setState(Mail.DEFAULT);

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/signed-opaque-validcertificate.eml"));

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("123@EXAMPLE.com"));

    mail.setRecipients(recipients);

    mail.setSender(new MailAddress("sender@example.com"));

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    TestUtils.saveMessages(tempDir, "testSMIMEAdaptMessageOverDirectLimit", mail.getMessage());

    assertTrue(mail.getMessage().isMimeType("multipart/mixed"));
    assertFalse(mail.getMessage() == message);

    assertEquals("test 1,test 2", mail.getMessage().getHeader("X-Test", ","));

    MimeMultipart mp = (MimeMultipart) mail.getMessage().getContent();

    assertEquals(2, mp.getCount());

    BodyPart bp = mp.getBodyPart(0);

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

    bp = mp.getBodyPart(1);

    assertTrue(bp.isMimeType("application/octet-stream"));
    assertEquals("attachment.smime", bp.getFileName());
}

From source file:org.bonitasoft.connectors.email.test.EmailConnectorTest.java

private List<byte[]> getAttachmentsContent(MimeMultipart multipart) throws MessagingException, IOException {
    List<byte[]> attachments = new ArrayList<byte[]>();
    for (int i = 0; i < multipart.getCount(); i++) {
        BodyPart bodyPart = multipart.getBodyPart(i);
        attachments.add(IOUtils.toByteArray(bodyPart.getInputStream()));
    }/*from w w w . j  av a  2  s  .co  m*/
    return attachments;
}

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

public static MimePart getMimePart(MimePart mp, String part) throws IOException, MessagingException {
    if (mp == null) {
        return null;
    }/*  w  ww.  j  av a  2s.c  o  m*/
    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;
}

From source file:org.alfresco.repo.imap.ImapMessageTest.java

public void testMessageRenamedBetweenReads() throws Exception {
    // Get test message UID
    final Long uid = getMessageUid(folder, 1);
    // Get Message size
    final int count = getMessageSize(folder, uid);

    // Get first part
    // Split the message into 2 part using a non multiple of 4 - 103 is a prime number
    // as the BASE64Decoder may not throw the IOException
    // see MNT-12995
    BODY body = getMessageBodyPart(folder, uid, 0, count - 103);

    // Rename message. The size of letter describing the node will change
    // These changes should be committed because it should be visible from client
    NodeRef contentNode = findNode(companyHomePathInStore + TEST_FILE);
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();/*from  w w w . jav a  2s. c o  m*/
    fileFolderService.rename(contentNode, "testtesttesttesttesttesttesttesttesttest");
    txn.commit();

    // Read second message part
    BODY bodyRest = getMessageBodyPart(folder, uid, count - 103, 103);

    // Creating and parsing message from 2 parts
    MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()),
            new SequenceInputStream(new BufferedInputStream(body.getByteArrayInputStream()),
                    new BufferedInputStream(bodyRest.getByteArrayInputStream())));

    // Reading first part - should be successful
    MimeMultipart content = (MimeMultipart) message.getContent();
    assertNotNull(content.getBodyPart(0).getContent());

    try {
        // Reading second part cause error
        content.getBodyPart(1).getContent();
        fail("Should raise an IOException");
    } catch (IOException e) {
    }
}