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

The following examples show how to use org.apache.james.mime4j.message.HeaderImpl. 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: 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: Email.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
public Email(){
	this.header = new HeaderImpl();
	this.attachments = new ArrayList<Attachment>();
	this.attachmentReplacedInHtmlBody = false;
	this.multipartStack = new Stack<MultipartType>();
	this.emailMessageStack = new Stack<EmailMessageType>();
	this.decodedEmailSize = 0;
	this.emailSize = 0;
}
 
Example #8
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 #9
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 #10
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 #11
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())));
    }
}