Read Multipart mail using Java Mail

This Example shows you how to read a multipart message using javamail api.

Read Multipart mail using Java Mail


This Example shows you how to read a multipart message using javamail api. When we read a multipart mail firstly create a Multipart class object.

These days  Multipart mail is used to compose emails with attachment.  In the email you can attach images, zip files, xls, doc etc.. It allows you to create nicely design emails.

Java Mail API also provides classes to create, send and read the Multipart message. If you have given a task to create emails with attachment in Java technologies, then you can use the Java Mail api to accomplish your task.

Using the Multipart Class

Following code snippet shows how you can use the Multipart class. You have to create the object of Multipart class and then you can get the body part and compose or read your email.

Multipart multipart = (Multipart) msg[i].getContent();After that create BodyPart object BodyPart bodyPart = multipart.getBodyPart(x);

And then read bodyPart content using getContent() method.

import java.util.*;
import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;

public class ReadMultipartMail {

  public static void main(String args[]) throws Exception {

  String host = "";
  String username = "komal";
  String passwoed = "komal";

  Properties properties = System.getProperties();
  Session session = Session.getDefaultInstance(properties);

  Store store = session.getStore("pop3");
  store.connect(host, username, passwoed);

  Folder folder = store.getFolder("inbox");

  if (!folder.exists()) {
  System.out.println("No INBOX...");
  Message[] msg = folder.getMessages();

  for (int i = 0; i < msg.length; i++) {
  System.out.println("------------ Message " + (i + 1) + " ------------");
  String from = InternetAddress.toString(msg[i].getFrom());
  if (from != null) {
  System.out.println("From: " + from);

  String replyTo = InternetAddress.toString(
  if (replyTo != null) {
  System.out.println("Reply-to: " + replyTo);
  String to = InternetAddress.toString(
  if (to != null) {
  System.out.println("To: " + to);

  String subject = msg[i].getSubject();
  if (subject != null) {
  System.out.println("Subject: " + subject);
  Date sent = msg[i].getSentDate();
  if (sent != null) {
  System.out.println("Sent: " + sent);

  System.out.println("Message : ");

  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))) {
  System.out.println("Mail have some attachment : ");

  DataHandler handler = bodyPart.getDataHandler();
  System.out.println("file name : " + handler.getName());
  } else {

Output :

------------ Message ------------
From: test@localhost
Reply-to: test@localhost
To: komal@localhost
Subject: MultiPart Mail
Sent: Fri Jul 18 18:21:42 IST 2008

Message : 
This is multipart mail and u read part1......
This is multipart mail and u read part2......

------------ Message ------------
From: test@localhost
Reply-to: test@localhost
To: komal@localhost
Subject: JavaMail Attachment
Sent: Fri Jul 18 19:03:32 IST 2008

Message : 
eml mail
Mail have some attachment : 
file name : message.eml

Download code

Share on Google+Share on Google+

Read Multipart mail using Java Mail

Posted on: November 16, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Read Multipart mail using Java Mail   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
August 26, 2011

I need the contents in html. how should i change the code? any help would be appreciated.
September 21, 2011
Code not reponsing

The above code for reading email parts and display them was not showing any error but it was also not showing any output or the content related to the email.
September 21, 2012
problem with the code

Hi, This code really helped me a lot to understand Javamail. However the output of this code returns the HTML code in message body! I couldnot figure how to solve this problem! Help
October 2, 2012
no message content

hello, i want to use it for a android app, but i can`t read the message: ------ Message 1 ------- From: ... ... Message: -> [email protected] Mail have some attachment : file name : anhang1.txt ...
October 17, 2012
download attachments using imap

how i can get attachment in my browser window using jsp
August 29, 2013
Not able to run the above code

i run this in code in Netbeans IDE.. while running the code i got the exception which is in below... : Exception in thread "main" javax.mail.MessagingException: Connect failed; nested exception is: Connection timed out: connect at com.sun.mail.pop3.POP3Store.protocolConnect( at javax.mail.Service.connect( at javax.mail.Service.connect( at readmultipartmail.ReadMultipartMail.main( Caused by: Connection timed out: connect at Method) at at at at at at at com.sun.mail.util.SocketFetcher.createSocket( at com.sun.mail.util.SocketFetcher.getSocket( at com.sun.mail.pop3.Protocol.<init> ( at com.sun.mail.pop3.POP3Store.getPort( at com.sun.mail.pop3.POP3Store.protocolConnect( ... 3 more Java Result: 1 BUILD SUCCESSFUL (total time: 21 seconds)