0

i want to delete messages from speicifc sender in my INBOX folder using java. Here i found this code to get all emails from gmail. How can i get the specific sender (sender@gmail.com) and delete it or move it to trash folder.

Thank you

Code:

import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.pop3.POP3Folder;
import java.util.Properties;
import javax.mail.Flags;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

public class CheckingMails {

   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];
         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("Text: " + message.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();
      }
   }

   public static void main(String[] args) {

      String host = "pop.gmail.com";// change accordingly
      String mailStoreType = "pop3";
      String username = "myemail@gmail.com";// change accordingly
      String password = "password";// change accordingly

      check(host, mailStoreType, username, password);

   }

}
samco cosma
  • 39
  • 1
  • 3
  • 11

1 Answers1

0

Using Gmail's RESTful API this is possible

Users.messages: list to get the messages

and

Users.messages: trash to send to trash

Ajay George
  • 11,759
  • 1
  • 40
  • 48
  • ok thank you, i followed the steps for gmail api and i don't see the client_sceret_code for my project – samco cosma Oct 08 '14 at 13:51
  • http://stackoverflow.com/questions/11295661/google-apis-console-missing-client-secret might be of help – Ajay George Oct 08 '14 at 15:14
  • Thank you for your help for client_secret_code but i don't want to use Gmail API, i want to do it programmatically with previous code, – samco cosma Oct 14 '14 at 18:28