Thursday, 7 March 2013

How to Read your email of gmail account using java mail api

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.*;
import javax.activation.*;

public class FetchMail
{

    public static void main(String[] args) {

        // SUBSTITUTE YOUR ISP's POP3 SERVER HERE!!!
        String host = "pop.gmail.com";
        // SUBSTITUTE YOUR USERNAME AND PASSWORD TO ACCESS E-MAIL HERE!!!
        String user = "your user name";   //eg.  "ishwar"
        String password = "your gmail password";   //e.g "ishwar123"
        // SUBSTITUTE YOUR SUBJECT SUBSTRING TO SEARCH HERE!!!
        String subjectSubstringToSearch = "Test E-Mail through Java";

       
        Properties props = new Properties();
        props.put("mail.pop3s.starttls.enable", "true");
         Session session = Session.getDefaultInstance(props,null);

        try
         {

            // Get a Store object
            Store store = session.getStore("pop3s");
            store.connect(host,995,user, password);

            // Get "INBOX"
            Folder fldr = store.getFolder("INBOX");
            fldr.open(Folder.READ_WRITE);
            int count = fldr.getMessageCount();
            System.out.println(count  + " total messages");

            // Message numebers start at 1
            for(int i = 1; i <= count; i++)
            {
                        // Get  a message by its sequence number
                Message m = fldr.getMessage(i);

                // Get some headers
                Date date = m.getSentDate();
                Address [] from = m.getFrom();
                String subj = m.getSubject();
                String mimeType = m.getContentType();
                System.out.println(date + "\t" + from[0] + "\t" + subj + "\t" + mimeType);
            }

            // Search for e-mails by some subject substring.
            String pattern = subjectSubstringToSearch;
            SubjectTerm st = new SubjectTerm(pattern);
            // Get some message references
            Message [] found = fldr.search(st);

            System.out.println(found.length +" messages matched Subject pattern \"" +pattern + "\"");

            for (int i = 0; i < found.length; i++)
            {
                Message m = found[i];
                // Get some headers of email
                Date date = m.getSentDate();
                Address [] from = m.getFrom();
                String subj = m.getSubject();
                String mimeType = m.getContentType();
                System.out.println(date + "\t" + from[0] + "\t" + subj + "\t" + mimeType);

                Object o = m.getContent();
               
                if (o instanceof Multipart)
                {
                    System.out.print("**This is a Multipart Message.  ");
                    Multipart mp = (Multipart)o;
                    int count3 = mp.getCount();
                    System.out.println("It has " + count3 +
                        " BodyParts in it**");
                    for (int j = 0; j < count3; j++)
                    {
                        // Part are numbered starting at 0
                        BodyPart b = mp.getBodyPart(j);
                        String mimeType2 = b.getContentType();
                        System.out.println( "BodyPart " + (j + 1) +
                                            " is of MimeType " + mimeType);

                        Object o2 = b.getContent();
                        if (o2 instanceof String)
                        {
                            System.out.println("**This is a String BodyPart**");
                            System.out.println((String)o2);
                        }
                        else if (o2 instanceof Multipart)
                        {
                            System.out.print(
                                "**This BodyPart is a nested Multipart.  ");
                            Multipart mp2 = (Multipart)o2;
                            int count2 = mp2.getCount();
                            System.out.println("It has " + count2 +
                                "further BodyParts in it**");
                        }
                        else if (o2 instanceof InputStream) {
                            System.out.println(
                                "**This is an InputStream BodyPart**");
                        }
                    } //End of for
                }
                else if (o instanceof String)
                {
                    System.out.println("**This is a String Message**");
                    System.out.println((String)o);
                }
                else if (o instanceof InputStream)
                {
                    System.out.println("**This is an InputStream message**");
                    InputStream is = (InputStream)o;
                    // Assumes character content (not binary images)
                    int c;
                    while ((c = is.read()) != -1)
                    {
                        System.out.write(c);
                    }
                }

             

            } //End of for

           
            fldr.close(true);
            store.close();

        }
        catch (MessagingException mex)
        {
           
            mex.printStackTrace();
        }
        catch (IOException ioex)
        {
            ioex.printStackTrace();
        }

    }


}
for above program you need to download two jar file
1)jaf-1.1.1
2)javamail-1.4.5
One more things you need to enable the POP protocol in your gmail account .

  1. You login in your gmail account.
  2. Click on the Gear icon in the top right-hand corner of your screen and select Mail settings.
  3. Click on Forwarding and POP/IMAP.
  4. Under POP Download/Status select the Enable option that you prefer.
  5. Under POP Download/When messages are accessed with POP select the option that you prefer. We recommend that you select one of the options that leaves a copy of your messages on the server. This allows you to view your e-mails from more than one location and also insures that you always have a backup on the Gmail servers.
  6. Click Save Changes.

1 comment: