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

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.

ReadMultipartMail.java


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 = "192.168.10.205";
  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...");
  System.exit(0);
  }
  folder.open(Folder.READ_WRITE);
  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(
  msg[i].getReplyTo());
  if (replyTo != null) {
  System.out.println("Reply-to: " + replyTo);
  }
  String to = InternetAddress.toString(
  msg[i].getRecipients(Message.RecipientType.TO));
  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();
  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 {
  System.out.println(bodyPart.getContent());
  }
  }
  System.out.println();
  }
  folder.close(true);
  store.close();
  }
} 

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