Java Code Examples for org.apache.james.mime4j.message.HeaderImpl#addField()

The following examples show how to use org.apache.james.mime4j.message.HeaderImpl#addField() . 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: MessageContentExtractorTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void extractShouldRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithoutCid() throws IOException {
    //Given
    BodyPart inlinedHTMLPart = BodyPartBuilder.create()
        .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8)
        .build();
    HeaderImpl inlinedHeader = new HeaderImpl();
    inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE));
    inlinedHeader.addField(Fields.contentType("text/html; charset=utf-8"));
    inlinedHTMLPart.setHeader(inlinedHeader);
    Multipart multipartAlternative = MultipartBuilder.create("alternative")
        .addBodyPart(inlinedHTMLPart)
        .build();
    Message message = Message.Builder.of()
        .setBody(multipartAlternative)
        .build();

    //When
    MessageContent actual = testee.extract(message);

    //Then
    assertThat(actual.getHtmlBody()).contains(HTML_CONTENT);
}
 
Example 2
Source File: MessageContentExtractorTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void extractShouldNotRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithCid() throws IOException {
    //Given
    BodyPart inlinedHTMLPart = BodyPartBuilder.create()
        .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8)
        .build();
    HeaderImpl inlinedHeader = new HeaderImpl();
    inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE));
    inlinedHeader.addField(Fields.contentType("text/html; charset=utf-8"));
    inlinedHeader.addField(CONTENT_ID_FIELD);
    inlinedHTMLPart.setHeader(inlinedHeader);
    Multipart multipartAlternative = MultipartBuilder.create("alternative")
        .addBodyPart(inlinedHTMLPart)
        .build();
    Message message = Message.Builder.of()
        .setBody(multipartAlternative)
        .build();

    //When
    MessageContent actual = testee.extract(message);

    //Then
    assertThat(actual.getHtmlBody()).isEmpty();
}
 
Example 3
Source File: MessageContentExtractorTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void extractShouldRetrieveTextBodyWithOneInlinedTextAttachmentWithoutCid() throws IOException {
    //Given
    BodyPart inlinedTextPart = BodyPartBuilder.create()
        .setBody(TEXT_CONTENT, "text", StandardCharsets.UTF_8)
        .build();
    HeaderImpl inlinedHeader = new HeaderImpl();
    inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE));
    inlinedHeader.addField(Fields.contentType("text/plain; charset=utf-8"));
    inlinedTextPart.setHeader(inlinedHeader);
    Multipart multipartAlternative = MultipartBuilder.create("alternative")
        .addBodyPart(inlinedTextPart)
        .build();
    Message message = Message.Builder.of()
        .setBody(multipartAlternative)
        .build();

    //When
    MessageContent actual = testee.extract(message);

    //Then
    assertThat(actual.getTextBody()).contains(TEXT_CONTENT);
}
 
Example 4
Source File: MessageContentExtractorTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void extractShouldNotRetrieveTextBodyWithOneInlinedTextAttachmentWithCid() throws IOException {
    //Given
    BodyPart inlinedTextPart = BodyPartBuilder.create()
        .setBody(TEXT_CONTENT, "text", StandardCharsets.UTF_8)
        .build();
    HeaderImpl inlinedHeader = new HeaderImpl();
    inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE));
    inlinedHeader.addField(Fields.contentType("text/plain; charset=utf-8"));
    inlinedHeader.addField(CONTENT_ID_FIELD);
    inlinedTextPart.setHeader(inlinedHeader);
    Multipart multipartAlternative = MultipartBuilder.create("alternative")
        .addBodyPart(inlinedTextPart)
        .build();
    Message message = Message.Builder.of()
        .setBody(multipartAlternative)
        .build();

    //When
    MessageContent actual = testee.extract(message);

    //Then
    assertThat(actual.getTextBody()).isEmpty();
}
 
Example 5
Source File: EnvelopeParseTest.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  header = new HeaderImpl();
  header.addField(Fields.bcc());
  header.addField(Fields.cc(ADDRESS2));
  header.addField(Fields.to(ADDRESS3));
  header.addField(Fields.date(Date.from(EnvelopeParser.parseDate("Mon, 29 Jan 2018 19:23:47 -0500").toInstant())));
  header.addField(Fields.from(ADDRESS1));
  header.addField(Fields.messageId("<CAKSL6jJTr_uwGqRDh4juaLLWdkmw0NyGcyiCYMFD15xm_A0=+w@mail.gmail.com>"));
  header.addField(Fields.subject("Multiple two field"));
}
 
Example 6
Source File: MessageSearches.java    From james-project with Apache License 2.0 5 votes vote down vote up
private HeaderImpl buildTextHeaders(MailboxMessage message) throws IOException, MimeIOException {
    DefaultMessageBuilder defaultMessageBuilder = new DefaultMessageBuilder();
    defaultMessageBuilder.setMimeEntityConfig(MimeConfig.PERMISSIVE);
    Message headersMessage = defaultMessageBuilder
        .parseMessage(message.getHeaderContent());
    HeaderImpl headerImpl = new HeaderImpl();
    addFrom(headerImpl, headersMessage.getFrom());
    addAddressList(headerImpl, headersMessage.getTo());
    addAddressList(headerImpl, headersMessage.getCc());
    addAddressList(headerImpl, headersMessage.getBcc());
    headerImpl.addField(Fields.subject(headersMessage.getSubject()));
    return headerImpl;
}
 
Example 7
Source File: StoreMessageManager.java    From james-project with Apache License 2.0 5 votes vote down vote up
private HeaderImpl readHeader(MimeTokenStream parser) throws IOException, MimeException {
    final HeaderImpl header = new HeaderImpl();

    EntityState next = parser.next();
    while (next != EntityState.T_BODY && next != EntityState.T_END_OF_STREAM && next != EntityState.T_START_MULTIPART) {
        if (next == EntityState.T_FIELD) {
            header.addField(parser.getField());
        }
        next = parser.next();
    }
    return header;
}
 
Example 8
Source File: MessageSearches.java    From james-project with Apache License 2.0 4 votes vote down vote up
private void addFrom(HeaderImpl headerImpl, MailboxList from) {
    if (from != null) {
        headerImpl.addField(Fields.from(Lists.newArrayList(from.iterator())));
    }
}
 
Example 9
Source File: MessageSearches.java    From james-project with Apache License 2.0 4 votes vote down vote up
private void addAddressList(HeaderImpl headerImpl, AddressList addressList) {
    if (addressList != null) {
        headerImpl.addField(Fields.to(Lists.newArrayList(addressList.iterator())));
    }
}