简体   繁体   中英

How to parse raw mime content in java?

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM