use of org.apache.james.mime4j.dom.MessageBuilder 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);
}
}
use of org.apache.james.mime4j.dom.MessageBuilder 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();
}
use of org.apache.james.mime4j.dom.MessageBuilder 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();
}
use of org.apache.james.mime4j.dom.MessageBuilder in project sling by apache.
the class MessageStoreImplRepositoryTest method assertSaveMessage.
private void assertSaveMessage(String messageFile) throws MimeException, IOException, FileNotFoundException {
MessageBuilder builder = new DefaultMessageBuilder();
Message msg = builder.parseMessage(new FileInputStream(new File(TU.TEST_FOLDER, messageFile)));
store.save(msg);
final Resource r = resolver.getResource(getResourcePath(msg, store));
assertNotNull("Expecting non-null Resource", r);
final ModifiableValueMap m = r.adaptTo(ModifiableValueMap.class);
File bodyFile = new File(TU.TEST_FOLDER, specialPathFromFilePath(messageFile, BODY_SUFFIX));
if (bodyFile.exists()) {
String expectedBody = readTextFile(bodyFile);
assertValueMap(m, "Body", expectedBody);
}
File headersFile = new File(TU.TEST_FOLDER, specialPathFromFilePath(messageFile, HEADERS_SUFFIX));
if (headersFile.exists()) {
MessageStoreImplRepositoryTestUtil.assertHeaders(headersFile, m);
}
// test at least something
assertTrue(headersFile.exists() || bodyFile.exists());
}
use of org.apache.james.mime4j.dom.MessageBuilder 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();
}
Aggregations