You can use the JavaMail mbox Store, but you'll need to build it yourself.
How to parse a Mbox file with JavaMail API?
-
04-07-2022 - |
Question
I want to use JavaMail to parse an .mbox file just like this one http://mail-archives.apache.org/mod_mbox/lucene-java-user/201210.mbox.
What I thought of doing was:
Session session = Session.getDefaultInstance(new Properties());
Store store = session.getStore("Here should go the .mbox file");
store.connect();
Folder folder = store.getFolder(server);
folder.open(Folder.READ_ONLY);
...
Which proved wrong. Any suggestions would be helpful.
Thank you in advance.
Update: Working example
public class MBoxFileReader implements MessageReader {
private final Path path; // Path to .mbox file
public MBoxFileReader(Path path) {
this.path = path;
}
@Override
public Message[] readMessages() {
Message[] messages = new Message[0];
URLName server = new URLName("mbox:" + path.toString());
Properties props = new Properties();
props.setProperty("mail.mime.address.strict", "false");
Session session = Session.getDefaultInstance(props);
try {
Folder folder = session.getFolder(server);
folder.open(Folder.READ_ONLY);
messages = folder.getMessages();
}
catch (Exception e) {
e.printStackTrace();
}
return messages;
}
}
Solution
OTHER TIPS
This can be done with Apache Mime4j:
CharsetEncoder ENCODER = Charset.forName("UTF-8").newEncoder();
final File mbox = new File(mboxPath);
for (CharBufferWrapper message : MboxIterator.fromFile(mbox).charset(ENCODER.charset()).build()) {
System.out.println(message);
}
you'll need:
<dependencies>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>apache-mime4j</artifactId>
<version>0.8.1</version>
<type>pom</type>
</dependency>
</dependencies>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow