Home Javamail Read Multipart mail using Java Mail
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Read Multipart mail using Java Mail

Advertisement
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.

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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 16, 2009

Related Tutorials

Ask Questions?    Discuss: Read Multipart mail using Java Mail   View All Comments

Post your Comment


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

I need the contents in html. how should i change the code? any help would be appreciated.
mohit
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.
yugesh
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
jom
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: -> javax.mail.internet.MimeMultipart@429805e0 Mail have some attachment : file name : anhang1.txt ...
Poonam
October 17, 2012
download attachments using imap

how i can get attachment in my browser window using jsp
Colsn
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: java.net.ConnectException: Connection timed out: connect at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:161) at javax.mail.Service.connect(Service.java:288) at javax.mail.Service.connect(Service.java:169) at readmultipartmail.ReadMultipartMail.main(ReadMultipartMail.java:27) Caused by: java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at java.net.Socket.connect(Socket.java:478) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189) at com.sun.mail.pop3.Protocol.<init> (Protocol.java:94) at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:214) at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:157) ... 3 more Java Result: 1 BUILD SUCCESSFUL (total time: 21 seconds)
DMCA.com