I am using JavaMail API for parsing email headers. In normal scenario with email file size in KBs, it wont eat much memory in JVM. But, in case of large email file (35MB+) with attachments, it use huge space in JVM.
Is there any possible way for email header modification without loading email content in momory?
I am doing something like this:
Properties props = System.getProperties();
Session mailSession = Session.getDefaultInstance(props, null);
InputStream source = new FileInputStream(emlFile);
MimeMessage message = new MimeMessage(mailSession, source);
message.addHeader("X-Header","HeaderValue1");
message.addHeader("Y-Header", "HeaderValue2");
message.saveChanges();
Enumeration headerschange = message.getAllHeaders();
StringBuilder headerString = new StringBuilder();
while (headerschange.hasMoreElements()) {
Header h = (Header) headerschange.nextElement();
headerString = headerString.append(h.getName() + ": " + h.getValue() + "\n");
}
System.out.println("headerString::::::::::::::::::::"+headerString.toString());
Mails as EML Files are stored as text files. A typical eml looks like this :
So you can read the file until two new lines found.
You find the exact specification here
http://www.ietf.org/rfc/rfc0822.txt