Reputation: 857
I have raw mime message which contains html content, inline images and attachments.
I want to parse and display the content in html page as like as mail client are displaying.
Is there any java library or methods available to parse the raw mime content ?
Upvotes: 1
Views: 8581
Reputation: 11234
Read message by using MimeMessage
and MimeMultipart
from
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.ByteArrayInputStream;
import java.util.Properties;
...
Properties props = System.getProperties();
Session session = Session.getInstance(props, null);
ByteArrayInputStream is = new ByteArrayInputStream(emailMessage.getBytes());
MimeMessage message = new MimeMessage(session, is);
if (message.getContent() instanceof String) {
return (String) message.getContent();
}
MimeMultipart content = (MimeMultipart)message.getContent();
if (content.getCount() <= 0) {
return null;
}
String contentString = (String) content.getBodyPart(0).getContent();
Upvotes: 0
Reputation: 6216
You need to read the file, then create a MimeMessage:
// read the file
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(new File(/*PATH*/)));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
fileData.append(buf, 0, numRead);
}
reader.close();
// Create a MimeMessage
Properties props = System.getProperties();
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session, new ByteArrayInputStream(fileData.toString().getBytes()));
Now that you have a mime message you can have access to its content using:
message.getContent();
The content type will depend on the mime type (could be a String, a Multipart object...)
Here is the JavaDoc for MimeMessage.
Upvotes: 3