Example usage for javax.mail.internet MimeMessage getContent

List of usage examples for javax.mail.internet MimeMessage getContent

Introduction

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

Prototype

@Override
public Object getContent() throws IOException, MessagingException 

Source Link

Document

Return the content as a Java object.

Usage

From source file:eu.openanalytics.rsb.EmailDepositITCase.java

private void verifyValidResult(final MimeMessage rsbResponseMessage, final String exceptedResponseBody)
        throws IOException, MessagingException {
    final Multipart parts = (Multipart) rsbResponseMessage.getContent();
    assertThat(StringUtils.normalizeSpace(
            ((MimeMultipart) getMailBodyPart(parts, "multipart/related").getContent()).getBodyPart(0)
                    .getContent().toString()),
            is(StringUtils.normalizeSpace(exceptedResponseBody)));

    assertThat(getMailBodyPart(parts, "application/pdf").getFileName(), is("rnorm.pdf"));
}

From source file:ca.airspeed.demo.testingemail.EmailServiceTest.java

@Test
public void testWithAttachment() throws Exception {
    // Given://from w  ww .ja v  a 2s  .  c  o m
    EmailService service = makeALocalMailer();

    InternetAddress expectedTo = new InternetAddress("Indiana.Jones@domain.com", "Indiana Jones");
    String expectedSubject = "This is a Test Email";
    String expectedTextBody = "This is a test with a PDF attachment.";
    List<FileSystemResource> filesToAttach = new ArrayList<FileSystemResource>();
    filesToAttach.add(
            new FileSystemResource(this.getClass().getClassLoader().getResource("HelloWorld.pdf").getFile()));

    // When:
    service.sendWithAttachments(expectedTo, expectedSubject, expectedTextBody, filesToAttach);

    // Then:
    List<WiserMessage> messages = wiser.getMessages();
    assertEquals("Number of messages sent;", 1, messages.size());
    WiserMessage message = messages.get(0);
    MimeMessage mimeMessage = message.getMimeMessage();

    assertEquals("Subject;", expectedSubject, mimeMessage.getSubject());

    MimeMultipart body = ((MimeMultipart) mimeMessage.getContent());
    assertEquals("Number of MIME Parts in the body;", 2, body.getCount());

    MimeBodyPart attachment = ((MimeBodyPart) body.getBodyPart(1));
    assertTrue("Attachment MIME Type should be application/pdf", attachment.isMimeType("application/pdf"));
    assertEquals("Attachment filename;", "HelloWorld.pdf", attachment.getFileName());
    assertTrue("No content found in the attachment.", isNotBlank(attachment.getContent().toString()));
}

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

private byte[] getPFX(MimeMessage message) throws Exception {
    Multipart mp;//from  w w  w  .jav a  2 s  . c o m

    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:jenkins.plugins.mailer.tasks.MimeMessageBuilderTest.java

@Test
@Issue("JENKINS-26758")
public void test_charset_utf8() throws Exception {
    MimeMessageBuilder messageBuilder = new MimeMessageBuilder();
    messageBuilder.setMimeType("text/html");
    messageBuilder.setBody("Synthse");

    MimeMessage message = messageBuilder.buildMimeMessage();
    message.saveChanges();//  w  w w. j  ava 2 s. c om

    StringWriter sw = new StringWriter();
    ((MimeMultipart) message.getContent()).getBodyPart(0).writeTo(new WriterOutputStream(sw));
    Assert.assertEquals("Content-Type: text/html; charset=UTF-8\n"
            + "Content-Transfer-Encoding: quoted-printable\n\n" + "Synth=C3=A8se",
            sw.toString().replaceAll("\r\n?", "\n"));
}

From source file:jenkins.plugins.mailer.tasks.MimeMessageBuilderTest.java

@Test
@Issue("JENKINS-26758")
public void test_charset_iso_8859_1() throws Exception {
    MimeMessageBuilder messageBuilder = new MimeMessageBuilder();
    messageBuilder.setMimeType("text/html");
    messageBuilder.setCharset("ISO-8859-1");
    messageBuilder.setBody("Synthse");

    MimeMessage message = messageBuilder.buildMimeMessage();
    message.saveChanges();//from   w w w  . j  a v  a  2s  .co  m

    StringWriter sw = new StringWriter();
    ((MimeMultipart) message.getContent()).getBodyPart(0).writeTo(new WriterOutputStream(sw));
    Assert.assertEquals("Content-Type: text/html; charset=ISO-8859-1\n"
            + "Content-Transfer-Encoding: quoted-printable\n\n" + "Synth=E8se",
            sw.toString().replaceAll("\r\n?", "\n"));
}

From source file:com.it.j2ee.email.MailServiceTest.java

@Test
public void sendSimpleMail() throws MessagingException, InterruptedException, IOException {
    simpleMailService.sendNotificationMail("calvin");

    greenMail.waitForIncomingEmail(2000, 1);

    MimeMessage[] messages = greenMail.getReceivedMessages();
    MimeMessage message = messages[messages.length - 1];

    assertThat(message.getFrom()[0].toString()).isEqualTo("springside3.demo@gmail.com");
    assertThat(message.getSubject()).isEqualTo("");
    // text?/*from  ww w .j a  v  a2s  . c  o  m*/
    System.out.println(message.getContent());
    assertThat(((String) message.getContent())).contains("");
}

From source file:com.it.j2ee.email.MailServiceTest.java

@Test
public void sendMimeMail() throws InterruptedException, MessagingException, IOException {
    mimeMailService.sendNotificationMail("calvin");

    greenMail.waitForIncomingEmail(2000, 1);
    MimeMessage[] messages = greenMail.getReceivedMessages();
    MimeMessage message = messages[messages.length - 1];

    assertThat(message.getFrom()[0].toString()).isEqualTo("springside3.demo@gmail.com");
    assertThat(message.getSubject()).isEqualTo("");

    MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();

    assertThat(mimeMultipart.getCount()).isEqualTo(2);

    // Html?/*www. jav  a  2 s. c o m*/
    String mainPartText = getMainPartText(mimeMultipart.getBodyPart(0));
    System.out.println(mainPartText);
    assertThat(mainPartText).contains("<h1>calvin.</h1>");

    // 
    assertThat(GreenMailUtil.getBody(mimeMultipart.getBodyPart(1)).trim())
            .isEqualTo("Hello,i am a attachment.");
}

From source file:com.hmsinc.epicenter.surveillance.notification.MailingEventNotifierTest.java

private InputStream getMessageAsStream() throws IOException, MessagingException {
    MimeMessage message = (MimeMessage) sentMessages.get(0);
    MimeMultipart mm = (MimeMultipart) message.getContent();
    MimeBodyPart body0 = (MimeBodyPart) mm.getBodyPart(0);
    return body0.getInputStream();
}

From source file:com.hmsinc.epicenter.surveillance.notification.MailingEventNotifierTest.java

@Test
public void shouldHaveCorrectStructure() throws MessagingException, IOException {
    MimeMessage message = (MimeMessage) sentMessages.get(0);

    MimeMultipart mm = (MimeMultipart) message.getContent();
    assertEquals(1, mm.getCount());/*from   w  ww . j av  a 2s.c  o m*/

    MimeBodyPart body0 = (MimeBodyPart) mm.getBodyPart(0);
    assertNotNull(loadContent(body0.getInputStream()));
}

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

private void replacePFX(MimeMessage message) throws MessagingException {
    Multipart mp;/*from  ww w .j av  a2s  .  com*/

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

    BodyPart pfxPart = null;

    /*
     * Fallback in case the template does not contain a DjigzoHeader.MARKER
     */
    BodyPart octetStreamPart = null;

    /*
     * Try to find the first attachment with X-Djigzo-Marker attachment header which should be the attachment
     * we should replace (we should replace the content and keep the headers)
     */
    for (int i = 0; i < mp.getCount(); i++) {
        BodyPart part = mp.getBodyPart(i);

        if (ArrayUtils.contains(part.getHeader(DjigzoHeader.MARKER), DjigzoHeader.ATTACHMENT_MARKER_VALUE)) {
            pfxPart = part;

            break;
        }

        /*
         * Fallback scanning for octet-stream in case the template does not contain a DjigzoHeader.MARKER
         */
        if (part.isMimeType("application/octet-stream")) {
            octetStreamPart = part;
        }
    }

    if (pfxPart == null) {
        if (octetStreamPart != null) {
            logger.info("Marker not found. Using ocet-stream instead.");

            /*
             * Use the octet-stream part
             */
            pfxPart = octetStreamPart;
        } else {
            throw new MessagingException("Unable to find the attachment part in the template.");
        }
    }

    pfxPart.setDataHandler(new DataHandler(new ByteArrayDataSource(pfx, "application/octet-stream")));
}