wood burning stoves 2.0*
The moose likes Other JSE/JEE APIs and the fly likes Problem in Reading email Body which contains Attachement. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Reply locked New topic

Problem in Reading email Body which contains Attachement.

Pankaj Kr. Singh
Ranch Hand

Joined: Mar 31, 2011
Posts: 32

Hi All,

I want to read the body message and attachment of mail. But when I start reading mail if the mail contains the attachment only attachment content is being read. I am not able to read the body message.

Below is the code.
public void readEmails()
Store store = null;
Folder folder = null;
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties, null);
store = session.getStore(PROTOCOL);
folder = store.getFolder(PROCESSFOLDER);
if (folder == null && !folder.exists())
System.out.println("Invalid folder ");
Message[] messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));

// For Starting
for (int i = 0; i < messages.length; i++)
Message msg = messages[i];
String contentType = msg.getContentType();
String textMsg = "";
if (contentType.contains("text/html") || contentType.contains("text/plain"))
textMsg = msg.getContent().toString();
System.out.println("Text Msg 111::::::::: " + textMsg);
} else if (contentType.contains("multipart"))
Multipart multiPart = (Multipart) msg.getContent();
int partCount = multiPart.getCount();
System.out.println("Multipart Count::::::::::: "+partCount);
for (int j = 0; j < partCount; j++)
BodyPart part = multiPart.getBodyPart(j);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()))

System.out.println("Attachment data::::::: "+part.getFileName());
System.out.println("Attachment data::::::: "+part.getContent().toString());

} else
// not an attachment, maybe the body content of the message
textMsg = part.getContent() != null ? part.getContent().toString() : "";
System.out.println("Text Msg::::::::: " + textMsg);

} catch (Exception ex)

Pankaj singh
Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

In your JavaMail download there's a "demo" directory. In that directory you will find a program called "msgshow". What does it output when you run it on that message?

Hint: if you like its output better than your program's output, well, you have its source code right there...
Rob Spoor

Joined: Oct 27, 2005
Posts: 19649

Duplicated at http://www.coderanch.com/t/593786/java/java/Reading-email-Body-which-contains. Closing this thread since the other one is resolved already.

Pankaj Singh Hari Om, please UseOneThreadPerQuestion.

How To Ask Questions How To Answer Questions
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Problem in Reading email Body which contains Attachement.
Similar Threads
Java mail attachmetnt include txt extensions
How can I download emails with attachments from Gmail with java programing..(error occur)
connection failure exception
Handling E-mails With Attachments
problem reading the mail Attachment