简体   繁体   English

如何解析java中的原始mime内容?

[英]How to parse raw mime content in java?

I have raw mime message which contains html content, inline images and attachments. 我有原始的mime消息,其中包含HTML内容,内嵌图像和附件。

I want to parse and display the content in html page as like as mail client are displaying. 我想在html页面中解析和显示内容,就像邮件客户端正在显示一样。

Is there any java library or methods available to parse the raw mime content ? 是否有可用于解析原始mime内容的java库或方法?

You need to read the file, then create a MimeMessage: 您需要读取该文件,然后创建一个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: 现在您有了一个mime消息,您可以使用以下内容访问其内容:

message.getContent();

The content type will depend on the mime type (could be a String, a Multipart object...) 内容类型将取决于mime类型(可以是String,Multipart对象......)

Here is the JavaDoc for MimeMessage. 这是MimeMessage的JavaDoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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