As the title suggests, I'm trying to import emails from a gmail account and it's working rather well, but I seem to have a problem in my code where the content of the message appears twice in my output.
here's my code :
private String processMessage(Message m) throws IOException, MessagingException {
String message = "";
StringBuilder sb = new StringBuilder();
Object o = m.getContent();
if (o instanceof Multipart) {
Multipart mm = (Multipart) m.getContent();
for (int i = 0; i < mm.getCount(); i++) {
BodyPart bp = mm.getBodyPart(i);
Object bpo = bp.getContent();
if (bpo instanceof String) {
sb.append(bpo);
}
}
} else if (o instanceof String) {
sb.append(o);
}
String htmlMessage = sb.toString();
message = htmlMessage.replaceAll("\\<.*?\\>", "");
return message;
}
This will return a nice formatted String containing the original text, but there will also be the complete message contained in a single line at the end of the String for some reason.
Say for the received message :
email
message
test
the output will be :
email
message
text
emailmessagetext
I'm guessing part of the multipart is a condensed version of the message content, but how can I avoid having this in the output?
P.S, if it's relevant, I'm getting the message by connecting to gmail via IMAP and then extracting all emails from a folder before processing them one by one.
folder.open(IMAPFolder.READ_ONLY);
messages = folder.getMessages();