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:org.apache.hupa.server.utils.TestUtils.java

public static ArrayList<String> summaryzeContent(Object content, String contentType, final int spaces)
        throws IOException, MessagingException {

    ContenTypeArrayList ret = new ContenTypeArrayList();

    ret.add(contentType, spaces);/*  ww  w . ja va  2 s . c  om*/
    if (content instanceof Multipart) {
        Multipart mp = (Multipart) content;
        contentType = mp.getContentType();
        for (int i = 0; i < mp.getCount(); i++) {
            Part part = mp.getBodyPart(i);
            contentType = part.getContentType();
            if (contentType.startsWith("text")) {
                ret.add(contentType, spaces + 1);
            } else if (contentType.startsWith("message/rfc822")) {
                MimeMessage msg = (MimeMessage) part.getDataHandler().getContent();
                ret.addAll(summaryzeContent(msg.getContent(), msg.getContentType(), spaces + 1));
            } else {
                if (part.getFileName() != null) {
                    ret.add(part.getContentType(), part.getFileName(), spaces + 1);
                } else {
                    ret.addAll(summaryzeContent(part.getContent(), contentType, spaces + 1));
                }
            }
        }
    }
    return ret;
}

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

@Test
public void serviceShouldSendMultipartMailContainingTextPart() throws Exception {
    FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName(MAILET_NAME)
            .mailetContext(fakeMailContext).build();
    dsnBounce.init(mailetConfig);/*from w  w w .  jav  a  2s  .c o m*/

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

    dsnBounce.service(mail);

    String hostname = InetAddress.getLocalHost().getHostName();
    String expectedContent = "Hi. This is the James mail server at " + hostname
            + ".\nI'm afraid I wasn't able to deliver your message to the following addresses.\nThis is a permanent error; I've given up. Sorry it didn't work out.  Below\nI include the list of recipients and the reason why I was unable to deliver\nyour message.\n\n"
            + "Failed recipient(s):\n" + "recipient@domain.com\n" + "\n" + "Error message:\n"
            + "Delivery error\n" + "\n";

    List<SentMail> sentMails = fakeMailContext.getSentMails();
    assertThat(sentMails).hasSize(1);
    SentMail sentMail = sentMails.get(0);
    MimeMessage sentMessage = sentMail.getMsg();
    MimeMultipart content = (MimeMultipart) sentMessage.getContent();
    BodyPart bodyPart = content.getBodyPart(0);
    assertThat(bodyPart.getContentType()).isEqualTo("text/plain; charset=us-ascii");
    assertThat(bodyPart.getContent()).isEqualTo(expectedContent);
}

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

@Test
public void serviceShouldSendMultipartMailContainingTextPartWhenCustomMessageIsConfigured() throws Exception {
    FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName(MAILET_NAME)
            .mailetContext(fakeMailContext).setProperty("messageString", "My custom message\n").build();
    dsnBounce.init(mailetConfig);/*from w  w  w . j a v  a  2s .co  m*/

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

    dsnBounce.service(mail);

    String expectedContent = "My custom message\n\n" + "Failed recipient(s):\n" + "recipient@domain.com\n"
            + "\n" + "Error message:\n" + "Delivery error\n" + "\n";

    List<SentMail> sentMails = fakeMailContext.getSentMails();
    assertThat(sentMails).hasSize(1);
    SentMail sentMail = sentMails.get(0);
    MimeMessage sentMessage = sentMail.getMsg();
    MimeMultipart content = (MimeMultipart) sentMessage.getContent();
    BodyPart bodyPart = content.getBodyPart(0);
    assertThat(bodyPart.getContentType()).isEqualTo("text/plain; charset=us-ascii");
    assertThat(bodyPart.getContent()).isEqualTo(expectedContent);
}

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

@Test
public void serviceShouldSendMultipartMailContainingDSNPart() throws Exception {
    FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName(MAILET_NAME)
            .mailetContext(fakeMailContext).build();
    dsnBounce.init(mailetConfig);/*from  w  w  w  .  ja va  2  s .  co  m*/

    MailAddress senderMailAddress = new MailAddress("sender@domain.com");
    FakeMail mail = FakeMail.builder().sender(senderMailAddress).attribute("delivery-error", "Delivery error")
            .mimeMessage(MimeMessageBuilder.mimeMessageBuilder().setText("My content")).name(MAILET_NAME)
            .recipient("recipient@domain.com").lastUpdated(Date.from(Instant.parse("2016-09-08T14:25:52.000Z")))
            .remoteAddr("remoteHost").build();

    dsnBounce.service(mail);

    String expectedContent = "Reporting-MTA: dns; myhost\n" + "Received-From-MTA: dns; 111.222.333.444\n" + "\n"
            + "Final-Recipient: rfc822; recipient@domain.com\n" + "Action: failed\n"
            + "Status: Delivery error\n" + "Diagnostic-Code: X-James; Delivery error\n"
            + "Last-Attempt-Date: Thu, 8 Sep 2016 14:25:52 XXXXX (UTC)\n";

    List<SentMail> sentMails = fakeMailContext.getSentMails();
    assertThat(sentMails).hasSize(1);
    SentMail sentMail = sentMails.get(0);
    MimeMessage sentMessage = sentMail.getMsg();
    MimeMultipart content = (MimeMultipart) sentMessage.getContent();
    SharedByteArrayInputStream actualContent = (SharedByteArrayInputStream) content.getBodyPart(1).getContent();
    assertThat(IOUtils.toString(actualContent, StandardCharsets.UTF_8)).isEqualTo(expectedContent);
}

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

@Test
public void serviceShouldNotAttachTheOriginalMailWhenAttachmentIsEqualToNone() throws Exception {
    FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName(MAILET_NAME)
            .mailetContext(fakeMailContext).setProperty("attachment", "none").build();
    dsnBounce.init(mailetConfig);//from ww w . ja  va  2s. com

    MailAddress senderMailAddress = new MailAddress("sender@domain.com");
    FakeMail mail = FakeMail.builder().sender(senderMailAddress)
            .mimeMessage(MimeMessageBuilder.mimeMessageBuilder().setText("My content")).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();
    assertThat(content.getCount()).isEqualTo(2);
}

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

@Test
public void serviceShouldAttachTheOriginalMailWhenAttachmentIsEqualToAll() throws Exception {
    FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName(MAILET_NAME)
            .mailetContext(fakeMailContext).setProperty("attachment", "all").build();
    dsnBounce.init(mailetConfig);//from   w ww. j a va  2 s. com

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

    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();

    assertThat(sentMail.getMsg().getContentType()).startsWith("multipart/report;");
    assertThat(MimeMessageUtil.asString((MimeMessage) content.getBodyPart(2).getContent()))
            .isEqualTo(MimeMessageUtil.asString(mimeMessageCopy));
}

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 w w  w.  j a v  a  2 s  .c om*/

    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.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 {//from  w  w  w .ja v  a2 s .c  om
            assertThat(IOUtils.toString((ByteArrayInputStream) multipart.getBodyPart(i).getContent()).trim())
                    .isEqualTo(contents[i]);
        }
    }
}

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

private void ensureResponseContains(String subject, String... contents) throws MessagingException, IOException {
    MimeMessage result = verifyHeaders(subject);
    MimeMultipart multipart = (MimeMultipart) result.getContent();
    assertThat(((String) multipart.getBodyPart(0).getContent()).split("\r\n")).containsOnly(contents);
}

From source file:org.apache.jmeter.assertions.SMIMEAssertion.java

public static AssertionResult getResult(SMIMEAssertionTestElement testElement, SampleResult response,
        String name) {// w  w w . jav  a2s .  c o m
    checkForBouncycastle();
    AssertionResult res = new AssertionResult(name);
    try {
        MimeMessage msg = null;
        final int msgPos = testElement.getSpecificMessagePositionAsInt();
        if (msgPos < 0) { // means counting from end
            SampleResult[] subResults = response.getSubResults();
            final int pos = subResults.length + msgPos;
            if (log.isDebugEnabled()) {
                log.debug("Getting message number: " + pos + " of " + subResults.length);
            }
            msg = getMessageFromResponse(response, pos);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Getting message number: " + msgPos);
            }
            msg = getMessageFromResponse(response, msgPos);
        }

        SMIMESignedParser s = null;
        if (log.isDebugEnabled()) {
            log.debug("Content-type: " + msg.getContentType());
        }
        if (msg.isMimeType("multipart/signed")) { // $NON-NLS-1$
            MimeMultipart multipart = (MimeMultipart) msg.getContent();
            s = new SMIMESignedParser(new BcDigestCalculatorProvider(), multipart);
        } else if (msg.isMimeType("application/pkcs7-mime") // $NON-NLS-1$
                || msg.isMimeType("application/x-pkcs7-mime")) { // $NON-NLS-1$
            s = new SMIMESignedParser(new BcDigestCalculatorProvider(), msg);
        }

        if (null != s) {
            log.debug("Found signature");

            if (testElement.isNotSigned()) {
                res.setFailure(true);
                res.setFailureMessage("Mime message is signed");
            } else if (testElement.isVerifySignature() || !testElement.isSignerNoCheck()) {
                res = verifySignature(testElement, s, name);
            }

        } else {
            log.debug("Did not find signature");
            if (!testElement.isNotSigned()) {
                res.setFailure(true);
                res.setFailureMessage("Mime message is not signed");
            }
        }

    } catch (MessagingException e) {
        String msg = "Cannot parse mime msg: " + e.getMessage();
        log.warn(msg, e);
        res.setFailure(true);
        res.setFailureMessage(msg);
    } catch (CMSException e) {
        res.setFailure(true);
        res.setFailureMessage("Error reading the signature: " + e.getMessage());
    } catch (SMIMEException e) {
        res.setFailure(true);
        res.setFailureMessage("Cannot extract signed body part from signature: " + e.getMessage());
    } catch (IOException e) { // should never happen
        log.error("Cannot read mime message content: " + e.getMessage(), e);
        res.setError(true);
        res.setFailureMessage(e.getMessage());
    }

    return res;
}