使用javamail读取电子邮件的html正文

时间:2011-08-27 04:24:23

标签: java javamail

我正在尝试获取包含标签等的html电子邮件的内容。现在我的代码只返回文本。这是我的代码:

    Store store = session.getStore("pop3");
    store.connect(host, username, passwoed);

    Folder folder = store.getFolder("Inbox");

    if (!folder.exists()) {
        System.out.println("No INBOX...");
        System.exit(0);
    }

    folder.open(Folder.READ_WRITE);
    Message[] msg = folder.getMessages();

    for (int i = msg.length - 1; i > 0; i--) {
        String sent1 = df.format(sent);
        sent1 = sent1.trim();
        int index11 = sent1.indexOf(DateTime);
        if (index11 != -1) {
            String to = InternetAddress.toString(msg[i].getRecipients(Message.RecipientType.TO));
            String s1 = "";

            try {

                Multipart multipart = (Multipart) msg[i].getContent();

                for (int x = 0; x < multipart.getCount(); x++) {
                    BodyPart bodyPart = multipart.getBodyPart(x);

                    String disposition = bodyPart.getDisposition();

                    if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {


                        DataHandler handler = bodyPart.getDataHandler();

                        s1 = (String) bodyPart.getContent();
                    } else {

                        s1 = (String) bodyPart.getContent();
                    }

                }

            }
        }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:9)

您可以找到Content-Type: TEXT/HTML这样的邮件:

Object content = message.getContent();
if (content instanceof Multipart) {
    Multipart mp = (Multipart) content;
    for (int i = 0; i < mp.getCount(); i++) {
        BodyPart bp = mp.getBodyPart(i);
        if (Pattern
                .compile(Pattern.quote("text/html"),
                        Pattern.CASE_INSENSITIVE)
                .matcher(bp.getContentType()).find()) {
            // found html part
            System.out.println((String) bp.getContent());
        } else {
            // some other bodypart...
        }
    }
}

<强>输出:

<H1>Hi there</H1><p>Bye.</p>