Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to get the contents of an html email including the tags etc. right now my code only returns the texts.this is my code:

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

                }

            }
        }

any help would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
408 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can find a mail with Content-Type: TEXT/HTML like this:

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...
        }
    }
}

Output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...