Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read mails again and again from gmail using JavaMail Api in java

Tags:

java

gmail-api

I am using JavaMail Api to read mails from gmail account. But problem is that I can read it only once. Is there any way to read the mails again and again???
My Java Code is :

import java.util.Properties;
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;

public class Main {

    // main function. Project run starts from main function...
   public static void main(String[] args) {

      String host = "pop.gmail.com";// change accordingly
      String mailStoreType = "pop3";
      String username = "[email protected]";// change accordingly
      String password = "your_password";// change accordingly

      check(host, mailStoreType, username, password);
   }


   // function to make connection and get mails from server known as "Pop" server
   public static void check(String host, String storeType, String user, String password) 
   {
      try {

      //create properties field
      Properties properties = new Properties();

      properties.put("mail.pop3.host", host);
      properties.put("mail.pop3.port", "995");
      properties.put("mail.pop3.starttls.enable", "true");
      Session emailSession = Session.getDefaultInstance(properties);

      //create the POP3 store object and connect with the pop server
      Store store = emailSession.getStore("pop3s");

      store.connect(host, user, password);

      //create the folder object and open it
      Folder emailFolder = store.getFolder("Inbox");

      emailFolder.open(Folder.READ_ONLY);

      // retrieve the messages from the folder in an array and print it
      Message[] messages = emailFolder.getMessages();
      System.out.println("messages.length---" + messages.length);

      for (int i = 0, n = messages.length; i < n; i++) {
         Message message = messages[i];


         Object obj = message.getContent();
         Multipart mp = (Multipart)obj;
         BodyPart bp = mp.getBodyPart(0);


         System.out.println("---------------------------------");
         System.out.println("Email Number " + (i + 1));
         System.out.println("Subject: " + message.getSubject());
         System.out.println("From: " + message.getFrom()[0]);
         System.out.println("To: " + message.getAllRecipients().toString());
         System.out.println("Received Date:" + message.getReceivedDate());
         System.out.println("Text: " + bp.getContent().toString());
      }

      //close the store and folder objects
      emailFolder.close(false);
      store.close();

      } catch (NoSuchProviderException e) {
         e.printStackTrace();
      } catch (MessagingException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
} 

In this code I am using pop server to read emails from and now I give email and password in it and run. It works fine but it reads an email once only, next time if I run program , kit gives me number of messages equal to 0... I want to read messages again and again as many times as I want... Any help will be appreciated...

like image 418
Khawaja M. Awais Avatar asked Nov 04 '15 06:11

Khawaja M. Awais


1 Answers

If you want to get All Emails every time, IMAP sever will be best for it.

You can change the mail server to

IMAP.gmail.com

and the port will be 993 (considering you are using gmail account).

The Link sidgate provided will be best example for you.

like image 110
Awais Dar Avatar answered Oct 15 '22 08:10

Awais Dar