Receiving Email With Attachment Using JavaMail API

This article explains how to receive emails using the JavaMail API and a basic example.

About JavaMail API

All the basics about JavaMail API can be read from my previous article where all the concepts, definitions, packages, classes have been explained in detail that will be available at the following link:

How to Send Email with Attachment using JavaMail API

The following explains how to receive the mail using the JavaMail API.

For receiving mail we need to use all the core classes of packages used such as javax.mail, javax.mail.internet, javax.mail.activation, java.io, and so on.

When we receive the mail, we also sometimes receive attachments with the mail and that is done using the multipart and BodyPart classes.

During the receiving of the mail also, we need to load the following two jar files:

  • Mail.jar
  • Activation.jar

Without these jar files, your packages (javax.mail and javax.mail.internet) will not work and hence the mail will also not be received. Both the jar files have been attached to the previous article from where you can download those files or you can also download from the Oracle website.

Now concentrate on the following example of receiving the mail using the JavaMail API.

Example of receiving mail

Here we have created some classes that are different from that used in the example of sending mail. So read and analyse the difference between the two.

import javax.mail.*;

import javax.activation.*;

import javax.mail.internet.*;

import java.util.*;

import java.io.*;

 

class ReadAttachment

{

    public static void main(String agrs[])throws Exception

    {

        String host="mail.c-sharpcorner.com"; //you can change according to your choice

        final String user="[email protected]"; //you can change according to your choice

        final String password="xxxxxxx"; //you can change according to your choice

 

        Properties properties=System.getProperties(); //getting the session object

        properties.setProperty("mail.smtp.host",host);

        properties.put("mail.smtp.auth","true");

 

        Session session=Session.getDefaultInstance(properties,new javax.mail.Authenticator()

        {

            protected PasswordAuthentication getPassworAuthentication()

            {

                return new PasswordAuthentication(user,password);

            }

        });

        Store store=session.getStore("pop3");

        store.connect(host, user, password);

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

        folder.open(Folder.READ_WRITE); 

        Message[] message=folder.getMessages();

 

        for(int i=0;i<message.length;i++)

        {

            System.out.println("-------"+(i+1)+"-------");

            System.out.println(message[i].getSentDate());

            Multipart multipart=(Multipart)message[i].getContent();

            for(int j=0;j<multipart.getCount();j++)

            {

                BodyPart bodyPart=multipart.getBodyPart(i);

                InputStream stream=bodyPart.getInputStream();

                BufferedReader br=new BufferedReader(new InputStreamReader(stream));

                while (br.ready())

                {

                    System.out.println(br.readLine());

                }

                System.out.println();

            }

            System.out.println();

        }

        folder.close(true);

        store.close();

    }

}

This will help to receive the emails with attachment.

Up Next
    Ebook Download
    View all
    Learn
    View all