Example usage for javax.mail BodyPart getContent

List of usage examples for javax.mail BodyPart getContent

Introduction

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

Prototype

public Object getContent() throws IOException, MessagingException;

Source Link

Document

Return the content as a Java object.

Usage

From source file:org.apache.james.transport.mailets.DSNBounceTest.java

@Test
public void serviceShouldAttachTheOriginalMailHeadersOnlyWhenAttachmentIsEqualToHeads() throws Exception {
    FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName(MAILET_NAME)
            .mailetContext(fakeMailContext).setProperty("attachment", "heads").build();
    dsnBounce.init(mailetConfig);/*from   ww  w . j  av  a  2 s . c o m*/

    MailAddress senderMailAddress = new MailAddress("sender@domain.com");
    FakeMail mail = FakeMail.builder().sender(senderMailAddress)
            .mimeMessage(MimeMessageBuilder.mimeMessageBuilder().setText("My content")
                    .addHeader("myHeader", "myValue").setSubject("mySubject"))
            .name(MAILET_NAME).recipient("recipient@domain.com")
            .lastUpdated(Date.from(Instant.parse("2016-09-08T14:25:52.000Z"))).build();

    dsnBounce.service(mail);

    List<SentMail> sentMails = fakeMailContext.getSentMails();
    assertThat(sentMails).hasSize(1);
    SentMail sentMail = sentMails.get(0);
    assertThat(sentMail.getSender()).isNull();
    assertThat(sentMail.getRecipients()).containsOnly(senderMailAddress);
    MimeMessage sentMessage = sentMail.getMsg();
    MimeMultipart content = (MimeMultipart) sentMessage.getContent();
    BodyPart bodyPart = content.getBodyPart(2);
    SharedByteArrayInputStream actualContent = (SharedByteArrayInputStream) bodyPart.getContent();
    assertThat(IOUtils.toString(actualContent, StandardCharsets.UTF_8)).contains("Subject: mySubject")
            .contains("myHeader: myValue");
    assertThat(bodyPart.getContentType()).isEqualTo("text/rfc822-headers; name=mySubject");
}

From source file:org.apache.jmeter.protocol.mail.sampler.MailReaderSampler.java

private void appendMultiPart(SampleResult child, StringBuilder cdata, MimeMultipart mmp)
        throws MessagingException, IOException {
    String preamble = mmp.getPreamble();
    if (preamble != null) {
        cdata.append(preamble);//from  w w w . j av  a2 s. c  o m
    }
    child.setResponseData(cdata.toString(), child.getDataEncodingNoDefault());
    int count = mmp.getCount();
    for (int j = 0; j < count; j++) {
        BodyPart bodyPart = mmp.getBodyPart(j);
        final Object bodyPartContent = bodyPart.getContent();
        final String contentType = bodyPart.getContentType();
        SampleResult sr = new SampleResult();
        sr.setSampleLabel("Part: " + j);
        sr.setContentType(contentType);
        sr.setDataEncoding(RFC_822_DEFAULT_ENCODING);
        sr.setEncodingAndType(contentType);
        sr.sampleStart();
        if (bodyPartContent instanceof InputStream) {
            sr.setResponseData(IOUtils.toByteArray((InputStream) bodyPartContent));
        } else if (bodyPartContent instanceof MimeMultipart) {
            appendMultiPart(sr, cdata, (MimeMultipart) bodyPartContent);
        } else {
            sr.setResponseData(bodyPartContent.toString(), sr.getDataEncodingNoDefault());
        }
        sr.setResponseOK();
        if (sr.getEndTime() == 0) {// not been set by any child samples
            sr.sampleEnd();
        }
        child.addSubResult(sr);
    }
}

From source file:org.ikasan.component.endpoint.email.producer.EmailProducerTest.java

@Test
public void test_successful_email_withoutAttachment() throws MessagingException, IOException {
    EmailProducerConfiguration emailProducerConfiguration = getConfiguration(false, null);

    EmailProducer emailProducer = new EmailProducer();
    ((Configured) emailProducer).setConfiguration(emailProducerConfiguration);
    ((ManagedResource) emailProducer).startManagedResource();

    emailProducer.invoke(getEmailPayload(false, null));
    List<WiserMessage> messages = wiser.getMessages();

    Assert.assertTrue("Should be three messages - one per addressee", messages.size() == 3);
    for (WiserMessage message : wiser.getMessages()) {
        Assert.assertTrue("sender should be " + sender, sender.equals(message.getEnvelopeSender()));

        MimeMessage mimeMessage = message.getMimeMessage();
        MimeMultipart mimeMultipart = (MimeMultipart) mimeMessage.getContent();
        Assert.assertTrue("should be only 1 bodypart", mimeMultipart.getCount() == 1);
        BodyPart bodyPart = mimeMultipart.getBodyPart(0);
        String content = (String) bodyPart.getContent();
        Assert.assertTrue("The email content should be empty", content.isEmpty());
        Assert.assertTrue("Should fild email format as \"text/plain\"",
                bodyPart.getContentType().contains("text/plain"));
    }/*ww  w.ja v  a2s.  c  o  m*/
}

From source file:org.ikasan.component.endpoint.email.producer.EmailProducerTest.java

@Test
public void test_successful_email_contentFromConfig() throws MessagingException, IOException {
    EmailProducerConfiguration emailProducerConfiguration = getConfiguration(false,
            "This content is from config");

    EmailProducer emailProducer = new EmailProducer();
    ((Configured) emailProducer).setConfiguration(emailProducerConfiguration);
    ((ManagedResource) emailProducer).startManagedResource();

    emailProducer.invoke(getEmailPayload(false, null));
    List<WiserMessage> messages = wiser.getMessages();

    Assert.assertTrue("Should be three messages - one per addressee", messages.size() == 3);
    for (WiserMessage message : wiser.getMessages()) {
        Assert.assertTrue("sender should be " + sender, sender.equals(message.getEnvelopeSender()));

        MimeMessage mimeMessage = message.getMimeMessage();
        MimeMultipart mimeMultipart = (MimeMultipart) mimeMessage.getContent();
        Assert.assertTrue("should be only 1 bodypart", mimeMultipart.getCount() == 1);
        BodyPart bodyPart = mimeMultipart.getBodyPart(0);
        String content = (String) bodyPart.getContent();
        Assert.assertEquals("The email content should be from config", "This content is from config", content);
        Assert.assertTrue("Should find email format as \"text/plain\"",
                bodyPart.getContentType().contains("text/plain"));
    }/*from w  w w. ja va  2 s .com*/
}

From source file:org.ikasan.component.endpoint.email.producer.EmailProducerTest.java

@Test
public void test_successful_email_contentFromPayload() throws MessagingException, IOException {
    EmailProducerConfiguration emailProducerConfiguration = getConfiguration(false,
            "This content is from config");

    EmailProducer emailProducer = new EmailProducer();
    ((Configured) emailProducer).setConfiguration(emailProducerConfiguration);
    ((ManagedResource) emailProducer).startManagedResource();

    emailProducer.invoke(getEmailPayload(false, "The content is from payload"));
    List<WiserMessage> messages = wiser.getMessages();

    Assert.assertTrue("Should be three messages - one per addressee", messages.size() == 3);
    for (WiserMessage message : wiser.getMessages()) {
        Assert.assertTrue("sender should be " + sender, sender.equals(message.getEnvelopeSender()));

        MimeMessage mimeMessage = message.getMimeMessage();
        MimeMultipart mimeMultipart = (MimeMultipart) mimeMessage.getContent();
        Assert.assertTrue("should be only 1 bodypart", mimeMultipart.getCount() == 1);
        BodyPart bodyPart = mimeMultipart.getBodyPart(0);
        String content = (String) bodyPart.getContent();
        Assert.assertEquals("The email content should be from payload", "The content is from payload", content);
        Assert.assertTrue("Should find email format as \"text/plain\"",
                bodyPart.getContentType().contains("text/plain"));
    }/*from w  w w  . j  av a2 s.c o m*/
}

From source file:org.ikasan.component.endpoint.email.producer.EmailProducerTest.java

@Test
public void test_successful_email_withAttachment() throws MessagingException, IOException {

    EmailProducerConfiguration emailProducerConfiguration = getConfiguration(true, null);

    EmailProducer emailProducer = new EmailProducer();
    ((Configured) emailProducer).setConfiguration(emailProducerConfiguration);
    ((ManagedResource) emailProducer).startManagedResource();

    emailProducer.invoke(getEmailPayload(true, null));
    List<WiserMessage> messages = wiser.getMessages();

    Assert.assertTrue("Should be three messages - one per addressee", messages.size() == 3);
    for (WiserMessage message : wiser.getMessages()) {
        Assert.assertTrue("sender should be " + sender, sender.equals(message.getEnvelopeSender()));

        MimeMessage mimeMessage = message.getMimeMessage();
        MimeMultipart mimeMultipart = (MimeMultipart) mimeMessage.getContent();
        Assert.assertTrue("should be 2 bodypart", mimeMultipart.getCount() == 2);
        BodyPart bodyPart = mimeMultipart.getBodyPart(0);
        String content = (String) bodyPart.getContent();
        Assert.assertTrue("The email content should be empty", content.isEmpty());
        BodyPart attachment = mimeMultipart.getBodyPart(1);
        Assert.assertEquals("Check attachment file name", "testAttachment", attachment.getFileName());
        Assert.assertTrue("Check file content",
                IOUtils.toString(attachment.getDataHandler().getDataSource().getInputStream())
                        .contains("1997,Ford,E350"));
        Assert.assertTrue("Should find email format as \"text/plain\"",
                bodyPart.getContentType().contains("text/plain"));

    }/*  w ww.  ja  v  a2  s .c om*/
}

From source file:org.obm.sync.server.mailer.EventChangeMailerTest.java

private void checkHtmlMessage(InvitationParts parts, List<String> expected)
        throws IOException, MessagingException {
    BodyPart htmlText = parts.htmlText;
    assertThat(htmlText.getContent()).isInstanceOf(String.class);
    String text = Jsoup.parse((String) htmlText.getContent()).text();
    checkStringContains(text, expected);
    checkStringDoesNotContains(text, getNoAllowedTimeFormat());
}

From source file:org.obm.sync.server.mailer.EventChangeMailerTest.java

private void checkPlainMessage(InvitationParts parts, List<String> expected)
        throws IOException, MessagingException {
    BodyPart plainText = parts.plainText;
    assertThat(plainText.getContent()).isInstanceOf(String.class);
    String text = (String) plainText.getContent();
    checkStringContains(text, expected);
    checkStringDoesNotContains(text, getNoAllowedTimeFormat());
}

From source file:org.obm.sync.server.mailer.EventChangeMailerTest.java

private void checkTextCalendar(BodyPart textCalendar, List<String> expected)
        throws IOException, MessagingException {
    assertThat(textCalendar.getContent()).isInstanceOf(String.class);
    String text = (String) textCalendar.getContent();
    checkStringContains(text, expected);
}

From source file:org.obm.sync.server.mailer.EventChangeMailerTest.java

private void checkApplicationIcs(BodyPart applicationIcs, List<String> expected)
        throws IOException, MessagingException {
    assertThat(applicationIcs.getContent()).isInstanceOf(SharedByteArrayInputStream.class);
    SharedByteArrayInputStream stream = (SharedByteArrayInputStream) applicationIcs.getContent();
    String decodedString = IOUtils.toString(stream, Charsets.US_ASCII.displayName());
    checkStringContains(decodedString, expected);
}