org.apache.james.mime4j.message.BasicBodyFactory Java Examples

The following examples show how to use org.apache.james.mime4j.message.BasicBodyFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldSetTextBodyWhenProvided() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);
    TextBody expected = new BasicBodyFactory().textBody("Hello all!", StandardCharsets.UTF_8);

    CreationMessage testMessage = CreationMessage.builder()
            .mailboxId("dead-bada55")
            .subject("subject")
            .from(DraftEmailer.builder().name("sender").build())
            .textBody("Hello all!")
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(), session);

    // Then
    assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset");
}
 
Example #2
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldSetEmptyBodyWhenNoBodyProvided() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);
    TextBody expected = new BasicBodyFactory().textBody("", StandardCharsets.UTF_8);

    CreationMessage testMessage = CreationMessage.builder()
            .mailboxId("dead-bada55")
            .subject("subject")
            .from(DraftEmailer.builder().name("sender").build())
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(), session);

    // Then
    assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset");
}
 
Example #3
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldSetHtmlBodyWhenProvided() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);
    TextBody expected = new BasicBodyFactory().textBody("Hello <b>all</b>!", StandardCharsets.UTF_8);

    CreationMessage testMessage = CreationMessage.builder()
            .mailboxId("dead-bada55")
            .subject("subject")
            .from(DraftEmailer.builder().name("sender").build())
            .htmlBody("Hello <b>all</b>!")
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(), session);

    // Then
    assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset");
}
 
Example #4
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldSetEmptyHtmlBodyWhenProvided() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);
    TextBody expected = new BasicBodyFactory().textBody("", StandardCharsets.UTF_8);

    CreationMessage testMessage = CreationMessage.builder()
            .mailboxId("dead-bada55")
            .subject("subject")
            .from(DraftEmailer.builder().name("sender").build())
            .htmlBody("")
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(), session);

    // Then
    assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset");
    assertThat(result.getMimeType()).isEqualTo("text/html");
}
 
Example #5
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldSetEmptyTextBodyWhenProvided() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);
    TextBody expected = new BasicBodyFactory().textBody("", StandardCharsets.UTF_8);

    CreationMessage testMessage = CreationMessage.builder()
            .mailboxId("dead-bada55")
            .subject("subject")
            .from(DraftEmailer.builder().name("sender").build())
            .textBody("")
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(), session);

    // Then
    assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset");
    assertThat(result.getMimeType()).isEqualTo("text/plain");
}
 
Example #6
Source File: ImapClientTest.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
@Test
public void testAppend() throws Exception {
  Header header = new HeaderImpl();
  header.addField(DefaultFieldParser.parse("Subject: This is the subject"));
  header.addField(DefaultFieldParser.parse("To: [email protected]"));
  header.addField(DefaultFieldParser.parse("From: [email protected]"));
  header.addField(DefaultFieldParser.parse("Date: 10-MAY-1994 00:00:00 -0000 (UTC)"));
  header.addField(DefaultFieldParser.parse("Message-ID: 12345"));

  Envelope envelope = new Envelope.Builder().setDate(ZonedDateTime.of(1994, 5, 10, 0, 0, 0, 0, ZoneId.of("UTC")));

  Body body = BasicBodyFactory.INSTANCE.textBody("This is a test");

  Message message = new MessageImpl();
  message.setBody(body);
  message.setHeader(header);

  ImapMessage imapMessage = new ImapMessage.Builder()
      .setFlags(ImmutableSet.of(StandardMessageFlag.SEEN, StandardMessageFlag.RECENT))
      .setEnvelope(envelope)
      .setBody(message);

  FetchResponse preAppendFetchAll = client.fetch(1, Optional.empty(), FetchDataItemType.UID, FetchDataItemType.FLAGS, FetchDataItemType.ENVELOPE, new BodyPeekFetchDataItem()).get();
  assertThat(preAppendFetchAll.getMessages().size()).isEqualTo(1);

  TaggedResponse appendResponse = client.append(DEFAULT_FOLDER, imapMessage.getFlags(), imapMessage.getEnvelope().getDate(), imapMessage).get();
  assertThat(appendResponse.getCode()).isEqualTo(ResponseCode.OK);
  long uid = Long.parseLong(appendResponse.getMessage().substring(25, 26));

  FetchResponse postAppendFetchAll = client.fetch(1, Optional.empty(), FetchDataItemType.UID, FetchDataItemType.ENVELOPE, new BodyPeekFetchDataItem()).get();
  assertThat(postAppendFetchAll.getMessages().size()).isEqualTo(2);

  FetchResponse postAppendFetchUid = client.uidfetch(uid, Optional.of(uid), FetchDataItemType.UID, FetchDataItemType.ENVELOPE, new BodyPeekFetchDataItem()).get();
  assertThat(postAppendFetchUid.getMessages().size()).isEqualTo(1);
  assertThat(postAppendFetchUid.getMessages().iterator().next().getBody().getSubject()).isEqualToIgnoringCase("This is the subject");
  assertThat(postAppendFetchUid.getMessages().iterator().next().getEnvelope().getMessageId()).isEqualToIgnoringCase("12345");
}
 
Example #7
Source File: MDN.java    From james-project with Apache License 2.0 5 votes vote down vote up
private Multipart asMime4JMultipart() throws IOException {
    MultipartBuilder builder = MultipartBuilder.create(REPORT_SUB_TYPE);
    builder.addContentTypeParameter(new NameValuePair("report-type", DISPOSITION_NOTIFICATION_REPORT_TYPE));
    builder.addBodyPart(BodyPartBuilder.create()
        .use(new BasicBodyFactory())
        .setBody(humanReadableText, Charsets.UTF_8)
        .setContentType("text/plain", UTF_8_CHARSET));
    builder.addBodyPart(BodyPartBuilder.create()
        .use(new BasicBodyFactory())
        .setBody(report.formattedValue(), Charsets.UTF_8)
        .setContentType(DISPOSITION_CONTENT_TYPE, UTF_8_CHARSET));

    return builder.build();
}
 
Example #8
Source File: MIMEMessageConverter.java    From james-project with Apache License 2.0 5 votes vote down vote up
private BodyPart attachmentBodyPart(MessageAttachmentMetadata att, MailboxSession session) throws IOException, AttachmentNotFoundException {
    try (InputStream attachmentStream = attachmentContentLoader.load(att.getAttachment(), session)) {
        BodyPartBuilder builder = BodyPartBuilder.create()
            .use(bodyFactory)
            .setBody(new BasicBodyFactory().binaryBody(ByteStreams.toByteArray(attachmentStream)))
            .setField(contentTypeField(att))
            .setField(contentDispositionField(att.isInline()))
            .setContentTransferEncoding(BASE64);
        contentId(builder, att);
        return builder.build();
    }
}
 
Example #9
Source File: MessageContentExtractorTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void extractShouldReturnEmptyWhenBinaryContentOnly() throws IOException {
    Message message = Message.Builder.of()
            .setBody(BasicBodyFactory.INSTANCE.binaryBody(BINARY_CONTENT, StandardCharsets.UTF_8))
            .build();
    MessageContent actual = testee.extract(message);
    assertThat(actual.getTextBody()).isEmpty();
    assertThat(actual.getHtmlBody()).isEmpty();
}
 
Example #10
Source File: DsnMailCreator.java    From mireka with Apache License 2.0 5 votes vote down vote up
private BodyPart humanReadableTextBodyPart() {
    BodyPart result = new BodyPart();
    TextBody textBody =
            new BasicBodyFactory().textBody(humanReadableText());
    result.setText(textBody);
    return result;
}
 
Example #11
Source File: DsnMailCreator.java    From mireka with Apache License 2.0 5 votes vote down vote up
private BodyPart deliveryStatusBodyPart() {
    BodyPart result = new BodyPart();
    TextBody textBody =
            new BasicBodyFactory().textBody(deliveryStatusText());
    result.setBody(textBody, "message/delivery-status");
    return result;
}
 
Example #12
Source File: MIMEMessageConverter.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Inject
public MIMEMessageConverter(AttachmentContentLoader attachmentContentLoader) {
    this.attachmentContentLoader = attachmentContentLoader;
    this.bodyFactory = new BasicBodyFactory();
}
 
Example #13
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Test
void convertToMimeShouldAddAttachment() throws Exception {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    CreationMessage testMessage = CreationMessage.builder()
        .mailboxId("dead-bada55")
        .subject("subject")
        .from(DraftEmailer.builder().name("sender").build())
        .htmlBody("Hello <b>all<b>!")
        .build();

    String expectedCID = "cid";
    String expectedMimeType = "image/png";
    String text = "123456";
    TextBody expectedBody = new BasicBodyFactory().textBody(text.getBytes(), StandardCharsets.UTF_8);
    AttachmentId blodId = AttachmentId.from("blodId");
    MessageAttachmentMetadata attachment = MessageAttachmentMetadata.builder()
        .attachment(AttachmentMetadata.builder()
            .attachmentId(blodId)
            .size(text.getBytes().length)
            .type(expectedMimeType)
            .build())
        .cid(Cid.from(expectedCID))
        .isInline(true)
        .build();
    when(attachmentContentLoader.load(attachment.getAttachment(), session))
        .thenReturn(new ByteArrayInputStream(text.getBytes()));

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
        CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment), session);
    Multipart typedResult = (Multipart)result.getBody();

    assertThat(typedResult.getBodyParts())
        .hasSize(1)
        .extracting(entity -> (Multipart) entity.getBody())
        .flatExtracting(Multipart::getBodyParts)
        .anySatisfy(part -> {
            assertThat(part.getBody()).isEqualToComparingOnlyGivenFields(expectedBody, "content");
            assertThat(part.getDispositionType()).isEqualTo("inline");
            assertThat(part.getMimeType()).isEqualTo(expectedMimeType);
            assertThat(part.getHeader().getField("Content-ID").getBody()).isEqualTo(expectedCID);
            assertThat(part.getContentTransferEncoding()).isEqualTo("base64");
        });
}
 
Example #14
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Test
void convertToMimeShouldPreservePartCharset() throws Exception {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    CreationMessage testMessage = CreationMessage.builder()
        .mailboxId("dead-bada55")
        .subject("subject")
        .from(DraftEmailer.builder().name("sender").build())
        .htmlBody("Hello <b>all<b>!")
        .build();

    String expectedCID = "cid";
    String expectedMimeType = "text/calendar; charset=\"iso-8859-1\"";
    String text = "123456";
    TextBody expectedBody = new BasicBodyFactory().textBody(text.getBytes(), StandardCharsets.UTF_8);
    AttachmentId blodId = AttachmentId.from("blodId");
    MessageAttachmentMetadata attachment = MessageAttachmentMetadata.builder()
        .attachment(AttachmentMetadata.builder()
            .attachmentId(blodId)
            .size(text.getBytes().length)
            .type(expectedMimeType)
            .build())
        .cid(Cid.from(expectedCID))
        .isInline(true)
        .build();
    when(attachmentContentLoader.load(attachment.getAttachment(), session))
        .thenReturn(new ByteArrayInputStream(text.getBytes()));

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
        CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment), session);
    Multipart typedResult = (Multipart)result.getBody();

    assertThat(typedResult.getBodyParts())
        .hasSize(1)
        .extracting(entity -> (Multipart) entity.getBody())
        .flatExtracting(Multipart::getBodyParts)
        .anySatisfy(part -> {
            assertThat(part.getBody()).isEqualToComparingOnlyGivenFields(expectedBody, "content");
            assertThat(part.getDispositionType()).isEqualTo("inline");
            assertThat(part.getMimeType()).isEqualTo("text/calendar");
            assertThat(part.getCharset()).isEqualTo("iso-8859-1");
            assertThat(part.getHeader().getField("Content-ID").getBody()).isEqualTo(expectedCID);
            assertThat(part.getContentTransferEncoding()).isEqualTo("base64");
        });
}
 
Example #15
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Test
void convertToMimeShouldAddAttachmentAndMultipartAlternativeWhenOneAttachementAndTextAndHtmlBody() throws Exception {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    CreationMessage testMessage = CreationMessage.builder()
        .mailboxId("dead-bada55")
        .subject("subject")
        .from(DraftEmailer.builder().name("sender").build())
        .textBody("Hello all!")
        .htmlBody("Hello <b>all<b>!")
        .build();
    TextBody expectedTextBody = new BasicBodyFactory().textBody("Hello all!".getBytes(), StandardCharsets.UTF_8);
    TextBody expectedHtmlBody = new BasicBodyFactory().textBody("Hello <b>all<b>!".getBytes(), StandardCharsets.UTF_8);

    String expectedCID = "cid";
    String expectedMimeType = "image/png";
    String text = "123456";
    TextBody expectedAttachmentBody = new BasicBodyFactory().textBody(text.getBytes(), StandardCharsets.UTF_8);
    MessageAttachmentMetadata attachment = MessageAttachmentMetadata.builder()
        .attachment(AttachmentMetadata.builder()
            .attachmentId(AttachmentId.from("blodId"))
            .size(text.getBytes().length)
            .type(expectedMimeType)
            .build())
        .cid(Cid.from(expectedCID))
        .isInline(true)
        .build();
    when(attachmentContentLoader.load(attachment.getAttachment(), session))
        .thenReturn(new ByteArrayInputStream(text.getBytes()));

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
        CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment), session);
    Multipart typedResult = (Multipart)result.getBody();

    assertThat(typedResult.getBodyParts())
        .hasSize(1)
        .extracting(entity -> (Multipart) entity.getBody())
        .flatExtracting(Multipart::getBodyParts)
        .satisfies(part -> {
            assertThat(part.getBody()).isInstanceOf(Multipart.class);
            assertThat(part.isMultipart()).isTrue();
            assertThat(part.getMimeType()).isEqualTo("multipart/alternative");
            assertThat(((Multipart)part.getBody()).getBodyParts()).hasSize(2);
            Entity textPart = ((Multipart)part.getBody()).getBodyParts().get(0);
            Entity htmlPart = ((Multipart)part.getBody()).getBodyParts().get(1);
            assertThat(textPart.getBody()).isEqualToComparingOnlyGivenFields(expectedTextBody, "content");
            assertThat(htmlPart.getBody()).isEqualToComparingOnlyGivenFields(expectedHtmlBody, "content");
        }, Index.atIndex(0))
        .satisfies(part -> {
            assertThat(part.getBody()).isEqualToComparingOnlyGivenFields(expectedAttachmentBody, "content");
            assertThat(part.getDispositionType()).isEqualTo("inline");
            assertThat(part.getMimeType()).isEqualTo(expectedMimeType);
            assertThat(part.getHeader().getField("Content-ID").getBody()).isEqualTo(expectedCID);
        }, Index.atIndex(1));
}