Search in sources :

Example 1 with DefaultMessageBuilder

use of org.apache.james.mime4j.message.DefaultMessageBuilder in project gerrit by GerritCodeReview.

the class RawMailParser method parse.

/**
   * Parses a MailMessage from a string.
   *
   * @param raw String as received over the wire
   * @return Parsed MailMessage
   * @throws MailParsingException
   */
public static MailMessage parse(String raw) throws MailParsingException {
    MailMessage.Builder messageBuilder = MailMessage.builder();
    messageBuilder.rawContentUTF(raw);
    Message mimeMessage;
    try {
        MessageBuilder builder = new DefaultMessageBuilder();
        mimeMessage = builder.parseMessage(new ByteArrayInputStream(raw.getBytes(UTF_8)));
    } catch (IOException | MimeException e) {
        throw new MailParsingException("Can't parse email", e);
    }
    // Add general headers
    if (mimeMessage.getMessageId() != null) {
        messageBuilder.id(mimeMessage.getMessageId());
    }
    if (mimeMessage.getSubject() != null) {
        messageBuilder.subject(mimeMessage.getSubject());
    }
    messageBuilder.dateReceived(new DateTime(mimeMessage.getDate()));
    // Add From, To and Cc
    if (mimeMessage.getFrom() != null && mimeMessage.getFrom().size() > 0) {
        Mailbox from = mimeMessage.getFrom().get(0);
        messageBuilder.from(new Address(from.getName(), from.getAddress()));
    }
    if (mimeMessage.getTo() != null) {
        for (Mailbox m : mimeMessage.getTo().flatten()) {
            messageBuilder.addTo(new Address(m.getName(), m.getAddress()));
        }
    }
    if (mimeMessage.getCc() != null) {
        for (Mailbox m : mimeMessage.getCc().flatten()) {
            messageBuilder.addCc(new Address(m.getName(), m.getAddress()));
        }
    }
    // Add additional headers
    mimeMessage.getHeader().getFields().stream().filter(f -> !MAIN_HEADERS.contains(f.getName().toLowerCase())).forEach(f -> messageBuilder.addAdditionalHeader(f.getName() + ": " + f.getBody()));
    // Add text and html body parts
    StringBuilder textBuilder = new StringBuilder();
    StringBuilder htmlBuilder = new StringBuilder();
    try {
        handleMimePart(mimeMessage, textBuilder, htmlBuilder);
    } catch (IOException e) {
        throw new MailParsingException("Can't parse email", e);
    }
    messageBuilder.textContent(Strings.emptyToNull(textBuilder.toString()));
    messageBuilder.htmlContent(Strings.emptyToNull(htmlBuilder.toString()));
    try {
        // required attributes are missing, so that the caller doesn't fall over.
        return messageBuilder.build();
    } catch (IllegalStateException e) {
        throw new MailParsingException("Missing required attributes after email was parsed", e);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Message(org.apache.james.mime4j.dom.Message) MimeException(org.apache.james.mime4j.MimeException) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) TextBody(org.apache.james.mime4j.dom.TextBody) Ints(com.google.common.primitives.Ints) InputStreamReader(java.io.InputStreamReader) Multipart(org.apache.james.mime4j.dom.Multipart) Strings(com.google.common.base.Strings) Address(com.google.gerrit.server.mail.Address) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) ImmutableList(com.google.common.collect.ImmutableList) ByteArrayInputStream(java.io.ByteArrayInputStream) CharStreams(com.google.common.io.CharStreams) Entity(org.apache.james.mime4j.dom.Entity) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Mailbox(org.apache.james.mime4j.dom.address.Mailbox) Message(org.apache.james.mime4j.dom.Message) Address(com.google.gerrit.server.mail.Address) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Mailbox(org.apache.james.mime4j.dom.address.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeException(org.apache.james.mime4j.MimeException)

Example 2 with DefaultMessageBuilder

use of org.apache.james.mime4j.message.DefaultMessageBuilder in project Gargoyle by callakrsos.

the class MimeToHtmlAdapter method getContent.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.kyj.fx.voeditor.visual.framework.word.AbstractMimeAdapter#getContent(
	 * )
	 */
@Override
public String getContent() {
    StringBuilder sb = new StringBuilder();
    try {
        final MessageBuilder builder = new DefaultMessageBuilder();
        Message message = builder.parseMessage(new ByteArrayInputStream(this.content));
        Body body = ((Entity) message).getBody();
        append(sb, body);
    } catch (MimeException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) StringBuilder(scala.collection.mutable.StringBuilder) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Message(org.apache.james.mime4j.dom.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) MimeException(org.apache.james.mime4j.MimeException) IOException(java.io.IOException) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) Body(org.apache.james.mime4j.dom.Body) TextBody(org.apache.james.mime4j.dom.TextBody)

Example 3 with DefaultMessageBuilder

use of org.apache.james.mime4j.message.DefaultMessageBuilder in project Gargoyle by callakrsos.

the class MimeToHtmlWordAdapter method getContent.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.kyj.fx.voeditor.visual.framework.word.AbstractMimeAdapter#getContent(
	 * )
	 */
@Override
public String getContent() {
    StringBuilder sb = new StringBuilder();
    try {
        final MessageBuilder builder = new DefaultMessageBuilder();
        Message message = builder.parseMessage(new ByteArrayInputStream(this.content));
        Body body = ((Entity) message).getBody();
        append(sb, body);
    } catch (MimeException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) StringBuilder(scala.collection.mutable.StringBuilder) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Message(org.apache.james.mime4j.dom.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) MimeException(org.apache.james.mime4j.MimeException) IOException(java.io.IOException) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) Body(org.apache.james.mime4j.dom.Body) TextBody(org.apache.james.mime4j.dom.TextBody)

Example 4 with DefaultMessageBuilder

use of org.apache.james.mime4j.message.DefaultMessageBuilder in project gerrit by GerritCodeReview.

the class RawMailParser method parse.

/**
 * Parses a MailMessage from a string.
 *
 * @param raw {@link String} payload as received over the wire
 * @return parsed {@link MailMessage}
 * @throws MailParsingException in case parsing fails
 */
public static MailMessage parse(String raw) throws MailParsingException {
    MailMessage.Builder messageBuilder = MailMessage.builder();
    messageBuilder.rawContentUTF(raw);
    Message mimeMessage;
    try {
        MessageBuilder builder = new DefaultMessageBuilder();
        mimeMessage = builder.parseMessage(new ByteArrayInputStream(raw.getBytes(UTF_8)));
    } catch (IOException | MimeException e) {
        throw new MailParsingException("Can't parse email", e);
    }
    // Add general headers
    if (mimeMessage.getMessageId() != null) {
        messageBuilder.id(mimeMessage.getMessageId());
    }
    if (mimeMessage.getSubject() != null) {
        messageBuilder.subject(mimeMessage.getSubject());
    }
    if (mimeMessage.getDate() != null) {
        @SuppressWarnings("JdkObsolete") Instant mimeMessageInstant = mimeMessage.getDate().toInstant();
        messageBuilder.dateReceived(mimeMessageInstant);
    }
    // Add From, To and Cc
    if (mimeMessage.getFrom() != null && !mimeMessage.getFrom().isEmpty()) {
        Mailbox from = mimeMessage.getFrom().get(0);
        messageBuilder.from(Address.create(from.getName(), from.getAddress()));
    }
    if (mimeMessage.getTo() != null) {
        for (Mailbox m : mimeMessage.getTo().flatten()) {
            messageBuilder.addTo(Address.create(m.getName(), m.getAddress()));
        }
    }
    if (mimeMessage.getCc() != null) {
        for (Mailbox m : mimeMessage.getCc().flatten()) {
            messageBuilder.addCc(Address.create(m.getName(), m.getAddress()));
        }
    }
    // Add additional headers
    mimeMessage.getHeader().getFields().stream().filter(f -> !MAIN_HEADERS.contains(f.getName().toLowerCase())).forEach(f -> messageBuilder.addAdditionalHeader(f.getName() + ": " + f.getBody()));
    // Add text and html body parts
    StringBuilder textBuilder = new StringBuilder();
    StringBuilder htmlBuilder = new StringBuilder();
    try {
        handleMimePart(mimeMessage, textBuilder, htmlBuilder);
    } catch (IOException e) {
        throw new MailParsingException("Can't parse email", e);
    }
    messageBuilder.textContent(Strings.emptyToNull(textBuilder.toString()));
    messageBuilder.htmlContent(Strings.emptyToNull(htmlBuilder.toString()));
    try {
        // required attributes are missing, so that the caller doesn't fall over.
        return messageBuilder.build();
    } catch (IllegalStateException e) {
        throw new MailParsingException("Missing required attributes after email was parsed", e);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Message(org.apache.james.mime4j.dom.Message) MimeException(org.apache.james.mime4j.MimeException) IOException(java.io.IOException) TextBody(org.apache.james.mime4j.dom.TextBody) Instant(java.time.Instant) Ints(com.google.common.primitives.Ints) InputStreamReader(java.io.InputStreamReader) Multipart(org.apache.james.mime4j.dom.Multipart) Strings(com.google.common.base.Strings) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) ImmutableList(com.google.common.collect.ImmutableList) ByteArrayInputStream(java.io.ByteArrayInputStream) CharStreams(com.google.common.io.CharStreams) Entity(org.apache.james.mime4j.dom.Entity) Address(com.google.gerrit.entities.Address) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Mailbox(org.apache.james.mime4j.dom.address.Mailbox) Message(org.apache.james.mime4j.dom.Message) Instant(java.time.Instant) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) IOException(java.io.IOException) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Mailbox(org.apache.james.mime4j.dom.address.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeException(org.apache.james.mime4j.MimeException)

Example 5 with DefaultMessageBuilder

use of org.apache.james.mime4j.message.DefaultMessageBuilder in project Gargoyle by callakrsos.

the class MimeViewerExam method start.

@Override
public void start(Stage primaryStage) throws Exception {
    WebView center = new WebView();
    WebEngine engine = center.getEngine();
    URL resource = MimeViewerExam.class.getResource("Sample.html");
    // String externalForm = resource.toExternalForm();
    String readToString = FileUtil.readToString(resource.openStream());
    System.out.println("##########################################");
    System.out.println("mime string");
    System.out.println(readToString);
    System.out.println("##########################################");
    final MessageBuilder builder = new DefaultMessageBuilder();
    final Message message = builder.parseMessage(resource.openStream());
    Body body = ((Entity) message).getBody();
    StringBuilder sb = new StringBuilder();
    extracted(sb, body);
    Document parse = Jsoup.parse(sb.toString());
    Elements select = parse.select("img");
    select.forEach(e -> {
        String attr = e.attr("src");
        if (attr.startsWith("cid:")) {
            String cid = attr.replace("cid:", "");
            String string = meta.get(cid);
            e.attr("src", "data:image/jpg;base64,".concat(string));
        }
    });
    System.out.println("##########################################");
    System.out.println("html string");
    System.out.println(sb.toString());
    System.out.println("##########################################");
    System.out.println(parse.toString());
    engine.loadContent(sb.toString(), "text/html");
    primaryStage.setScene(new Scene(new BorderPane(center)));
    primaryStage.show();
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) BorderPane(javafx.scene.layout.BorderPane) Message(org.apache.james.mime4j.dom.Message) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Scene(javafx.scene.Scene) WebEngine(javafx.scene.web.WebEngine) URL(java.net.URL) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) WebView(javafx.scene.web.WebView) Body(org.apache.james.mime4j.dom.Body) TextBody(org.apache.james.mime4j.dom.TextBody) BinaryBody(org.apache.james.mime4j.dom.BinaryBody)

Aggregations

Message (org.apache.james.mime4j.dom.Message)6 MessageBuilder (org.apache.james.mime4j.dom.MessageBuilder)6 DefaultMessageBuilder (org.apache.james.mime4j.message.DefaultMessageBuilder)6 Entity (org.apache.james.mime4j.dom.Entity)5 TextBody (org.apache.james.mime4j.dom.TextBody)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 MimeException (org.apache.james.mime4j.MimeException)4 BinaryBody (org.apache.james.mime4j.dom.BinaryBody)3 Body (org.apache.james.mime4j.dom.Body)3 Strings (com.google.common.base.Strings)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 CharStreams (com.google.common.io.CharStreams)2 Ints (com.google.common.primitives.Ints)2 InputStreamReader (java.io.InputStreamReader)2 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)2 Multipart (org.apache.james.mime4j.dom.Multipart)2 Mailbox (org.apache.james.mime4j.dom.address.Mailbox)2 StringBuilder (scala.collection.mutable.StringBuilder)2