Reputation: 251
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "xxx", "xxx");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();
for (int i = messages.length;i>=0; i--) {
Message message =messages[i];
System.out.println("Text: " + message.getContent().toString());
}
I am able to read emails and I am trying to get email content for each email.However getContent method returns junk value eg:(Text:javax.mail.internet.MimeMultipart@17ff24f). How do I get complete email content. Please help.
Upvotes: 0
Views: 759
Reputation: 9705
The call to message.getContent()
doesn't return "junk value", it just returns something that cannot be directly converted into a String
value.
That is because it's an object of class MimeMultipart
. This means the email you're reading consists of multiple 'parts'. You can downcast the result of message.getContent()
to a MimeMultipart
variable and analyse it:
MimeMultipart multipart = (MimeMultipart) message.getContent();
System.out.println("This message consists of " + multipart.getCount() + " parts");
for (int i = 0; i < multipart.getCount(); i++) {
// Note we're downcasting here, MimeBodyPart is the only subclass
// of BodyPart available in the Java EE spec.
MimeBodyPart bodyPart = (MimeBodyPart) multipart.getBodyPart(i);
System.out.println("Part " + i + " has type " + bodyPart.getContentType());
System.out.println("Part " + i " + has filename " + bodyPart.getFileName()); // if it was an attachment
// So on, so forth.
}
See the Javadoc of MimeBodyPart
for details on how to analyse the parts of the message.
Upvotes: 1