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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With