Java Code Examples for org.apache.james.mime4j.dom.Message#Builder

The following examples show how to use org.apache.james.mime4j.dom.Message#Builder . 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: MIMEMessageConverter.java    From james-project with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting Message convertToMime(ValueWithId.CreationMessageEntry creationMessageEntry, ImmutableList<MessageAttachmentMetadata> messageAttachments, MailboxSession session) {
    if (creationMessageEntry == null || creationMessageEntry.getValue() == null) {
        throw new IllegalArgumentException("creationMessageEntry is either null or has null message");
    }

    Message.Builder messageBuilder = Message.Builder.of();
    if (isMultipart(creationMessageEntry.getValue(), messageAttachments)) {
        messageBuilder.setBody(createMultipart(creationMessageEntry.getValue(), messageAttachments, session));
    } else {
        messageBuilder.setBody(createTextBody(creationMessageEntry.getValue()))
            .setContentTransferEncoding(QUOTED_PRINTABLE);
    }
    buildMimeHeaders(messageBuilder, creationMessageEntry.getValue(), messageAttachments);
    return messageBuilder.build();
}
 
Example 2
Source File: MIMEMessageConverter.java    From james-project with Apache License 2.0 5 votes vote down vote up
private void buildMimeHeaders(Message.Builder messageBuilder, CreationMessage newMessage, ImmutableList<MessageAttachmentMetadata> messageAttachments) {
    Optional<Mailbox> fromAddress = newMessage.getFrom().filter(DraftEmailer::hasValidEmail).map(this::convertEmailToMimeHeader);
    fromAddress.ifPresent(messageBuilder::setFrom);
    fromAddress.ifPresent(messageBuilder::setSender);

    messageBuilder.setReplyTo(newMessage.getReplyTo().stream()
            .map(this::convertEmailToMimeHeader)
            .collect(Collectors.toList()));
    messageBuilder.setTo(newMessage.getTo().stream()
            .filter(DraftEmailer::hasValidEmail)
            .map(this::convertEmailToMimeHeader)
            .collect(Collectors.toList()));
    messageBuilder.setCc(newMessage.getCc().stream()
            .filter(DraftEmailer::hasValidEmail)
            .map(this::convertEmailToMimeHeader)
            .collect(Collectors.toList()));
    messageBuilder.setBcc(newMessage.getBcc().stream()
            .filter(DraftEmailer::hasValidEmail)
            .map(this::convertEmailToMimeHeader)
            .collect(Collectors.toList()));
    messageBuilder.setSubject(newMessage.getSubject());
    messageBuilder.setMessageId(generateUniqueMessageId(fromAddress));

    // note that date conversion probably lose milliseconds!
    messageBuilder.setDate(Date.from(newMessage.getDate().toInstant()), TimeZone.getTimeZone(newMessage.getDate().getZone()));
    newMessage.getInReplyToMessageId()
        .ifPresent(id -> addHeader(messageBuilder, IN_REPLY_TO_HEADER, id));
    if (!isMultipart(newMessage, messageAttachments)) {
        newMessage.getHtmlBody().ifPresent(x -> messageBuilder.setContentType(HTML_MEDIA_TYPE, UTF_8_CHARSET));
    }
    newMessage.getHeaders().entrySet().stream()
        .filter(header -> ! header.getKey().trim().isEmpty())
        .filter(header -> ! LOWERCASED_COMPUTED_HEADERS.contains(header.getKey().toLowerCase(Locale.ENGLISH)))
        .forEach(header -> addMultivaluedHeader(messageBuilder, header.getKey(), header.getValue()));
}
 
Example 3
Source File: MDN.java    From james-project with Apache License 2.0 4 votes vote down vote up
public Message.Builder asMime4JMessageBuilder() throws IOException {
    Message.Builder messageBuilder = Message.Builder.of();
    messageBuilder.setBody(asMime4JMultipart());
    return messageBuilder;
}
 
Example 4
Source File: MessageManager.java    From james-project with Apache License 2.0 4 votes vote down vote up
public static AppendCommand from(Message.Builder builder) throws IOException {
    return builder().build(builder);
}
 
Example 5
Source File: MessageManager.java    From james-project with Apache License 2.0 4 votes vote down vote up
public AppendCommand build(Message.Builder messageBuilder) throws IOException {
    return build(messageBuilder.build());
}
 
Example 6
Source File: MIMEMessageConverter.java    From james-project with Apache License 2.0 4 votes vote down vote up
private void addMultivaluedHeader(Message.Builder messageBuilder, String fieldName, String multipleValues) {
    Splitter.on(MessageViewFactory.JMAP_MULTIVALUED_FIELD_DELIMITER).split(multipleValues)
        .forEach(value -> addHeader(messageBuilder, fieldName, value));
}
 
Example 7
Source File: MIMEMessageConverter.java    From james-project with Apache License 2.0 4 votes vote down vote up
private void addHeader(Message.Builder messageBuilder, String fieldName, String value) {
    FieldParser<UnstructuredField> parser = UnstructuredFieldImpl.PARSER;
    RawField rawField = new RawField(fieldName, value);
    messageBuilder.addField(parser.parse(rawField, DecodeMonitor.SILENT));
}