use of org.apache.james.mime4j.dom.Message 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.Message in project sling by apache.
the class Mime4jMboxParserImplCountTest method testParse.
@Test
public void testParse() throws IOException {
Iterator<Message> iter = parser.parse(new FileInputStream(new File(TU.TEST_FOLDER, filePath)));
int cnt = 0;
Set<Message> set = new HashSet<Message>();
while (iter.hasNext()) {
Message message = (Message) iter.next();
cnt++;
set.add(message);
}
assertEquals("Expecting correct number of messages parsed", expectedMessagesCount, cnt);
assertEquals("Expecting all messages unique", expectedMessagesCount, set.size());
}
use of org.apache.james.mime4j.dom.Message in project sling by apache.
the class Mime4jMboxParserImplTest method testMboxParsing.
@Test
public void testMboxParsing() throws IOException {
Iterator<Message> iter = parser.parse(new FileInputStream(new File(TU.TEST_FOLDER, WRONGBODY_MBOX)));
boolean fail = true;
int i = 1;
while (iter.hasNext()) {
final Message message = iter.next();
File bodyFile = new File(TU.TEST_FOLDER, specialPathFromFilePath(WRONGBODY_MBOX, "_bodyOf" + i, "txt"));
if (bodyFile.exists()) {
final String actual = getPlainBody(message);
final String expected = readTextFile(bodyFile);
assertEquals("Body #" + i, expected, actual);
fail = false;
}
i++;
}
if (fail) {
fail("No file with expected body.");
}
}
use of org.apache.james.mime4j.dom.Message 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.Message 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();
}
Aggregations