How to parse raw mime content in java?

馋奶兔 提交于 2019-12-24 15:36:59

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/19541792/how-to-parse-raw-mime-content-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!