Easy learning with example program codes

Receiving or fetching email with attachment using JavaMail API

We are explaining here an example of receiving or fetching email with attachment using JavaMail API. The Store, Folder, Multipart and BodyPart classes are used to receive an email with attachment using JavaMail API.

Steps of receiving or fetching email with attachment using JavaMail API:

  1. Get the Session object.
  2. Create the POP3 store object and connect to the pop store.
  3. Create the folder object and open it in your mailbox.
  4. Retrieve the messages from the folder object.
  5. Retrieve the Multipart object from message object.
  6. Retrieve the BodyPart object from the Multipart object.
  7. Close the folder and store objects.


import java.util.Properties; 
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
 * This class is used to receive email with attachment.
 * @author codesjava
public class ReceiveEmailWithAttachment { 
 public static void receiveEmail(String pop3Host,
    String mailStoreType, String userName, String password){
    //Set properties
    Properties props = new Properties();
    props.put("", "pop3");
    props.put("", pop3Host);
    props.put("mail.pop3.port", "995");
    props.put("mail.pop3.starttls.enable", "true");
    // Get the Session object.
    Session session = Session.getInstance(props);
    try {
        //Create the POP3 store object and connect to the pop store.
	Store store = session.getStore("pop3s");
	store.connect(pop3Host, userName, password);
	//Create the folder object and open it in your mailbox.
	Folder emailFolder = store.getFolder("INBOX");;
	//Retrieve the messages from the folder object.
	Message[] messages = emailFolder.getMessages();
	System.out.println("Total Message" + messages.length);
	//Iterate the messages
	for (int i = 0; i < messages.length; i++) {
	   Message message = messages[i];
	   Address[] toAddress = 
	     System.out.println("Details of Email Message " 
                                                   + (i + 1) + " :");  
	     System.out.println("Subject: " + message.getSubject());  
	     System.out.println("From: " + message.getFrom()[0]);  
	     //Iterate recipients 
	     System.out.println("To: "); 
	     for(int j = 0; j < toAddress.length; j++){
	     //Iterate multiparts
	     Multipart multipart = (Multipart) message.getContent();
	     for(int k = 0; k < multipart.getCount(); k++){
	       BodyPart bodyPart = multipart.getBodyPart(k);  
	       InputStream stream = 
                             (InputStream) bodyPart.getInputStream();  
	       BufferedReader bufferedReader = 
	    	   new BufferedReader(new InputStreamReader(stream));  
	        while (bufferedReader.ready()) {  
	   //close the folder and store objects
	} catch (NoSuchProviderException e) {
	} catch (MessagingException e){
	} catch (Exception e) {
 public static void main(String[] args) {
  String pop3Host = "";//change accordingly
  String mailStoreType = "pop3";	
  final String userName = "codesjava";//change accordingly
  final String password = "****";//change accordingly
  //call receiveEmail
  receiveEmail(pop3Host, mailStoreType, userName, password);


Details of Email Message 1 :
Subject: Give your profile a makeover.
From: Twitter <>
javawithease <>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 
<meta http-equiv="Content-Type" content="text/html; 
charset=utf-8" />
<meta name="viewport" content="width=device-width,
 minimum-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<body bgcolor="#e9edef" style="margin:0;padding:0;
<table cellpadding="0" cellspacing="0" border="0"
 width="100%" bgcolor="#e9edef" style="background-color:
<style type="text/css">
@media only screen and (max-device-width: 480px) {
width:100% !important;
width:100% !important;
span[class="headline_1"] {
font-size:28px !important;
td[class="body_text"] {
font-size:14px !important;
line-height:22px !important;
width:238px !important;
span[class="address"] a {
line-height:16px !important;
width:42px !important;
height:16px !important;
height:12px !important;
display:block !important;
width:240px !important;
<td align="center" style="padding:0;margin:0;

Download this example.
Next Topic: Replying emails with example.
Previous Topic: Receiving emails with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki