如何解析Java中的原始mime内容? [英] How to parse raw mime content in java?

查看:81
本文介绍了如何解析Java中的原始mime内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有原始的mime消息,其中包含html内容,嵌入式图像和附件.

I have raw mime message which contains html content, inline images and attachments.

我想解析和显示html页面中的内容,就像显示邮件客户端一样.

I want to parse and display the content in html page as like as mail client are displaying.

是否有任何Java库或方法可用于解析原始的mime内容?

Is there any java library or methods available to parse the raw mime content ?

推荐答案

您需要读取文件,然后创建MimeMessage:

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();

内容类型取决于mime类型(可以是String,Multipart对象...)

The content type will depend on the mime type (could be a String, a Multipart object...)

这是 JavaDoc 用于MimeMessage.

Here is the JavaDoc for MimeMessage.

这篇关于如何解析Java中的原始mime内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆