Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaMail reading recent unread mails using IMAP

Tags:

I have a requirement to retrieve unread mails from Gmail. I am using Java Mail API. By default, this API retrieves mails from the oldest to newest. But I need to retrieve recent mails first. Is it possible? Thanks in advance.

like image 677
Ram Bavireddi Avatar asked Feb 24 '15 05:02

Ram Bavireddi


People also ask

Which protocol is used to receive the messages in JavaMail?

IMAP: Acronym for Internet Message Access Protocol. It is an advanced protocol for receiving messages. It provides support for multiple mailbox for each user, in addition to, mailbox can be shared by multiple users. It is defined in RFC 2060.

How do I send an email to multiple recipients using JavaMail?

addRecipient(Message.RecipientType.CC, "[email protected],[email protected],[email protected]"); Or message. addRecipient(Message.RecipientType.CC, "[email protected];[email protected];[email protected]"); I can send a message using alternate methods too, but I want to know the purpose of the above method.


2 Answers

Here is example. Do not forget to add javax.mail in your classpath.

import javax.mail.*; import javax.mail.search.FlagTerm; import java.util.*;  public class GmailFetch {    public static void main( String[] args ) throws Exception {      Session session = Session.getDefaultInstance(new Properties( ));     Store store = session.getStore("imaps");     store.connect("imap.googlemail.com", 993, "[email protected]", "password");     Folder inbox = store.getFolder( "INBOX" );     inbox.open( Folder.READ_ONLY );      // Fetch unseen messages from inbox folder     Message[] messages = inbox.search(         new FlagTerm(new Flags(Flags.Flag.SEEN), false));      // Sort messages from recent to oldest     Arrays.sort( messages, ( m1, m2 ) -> {       try {         return m2.getSentDate().compareTo( m1.getSentDate() );       } catch ( MessagingException e ) {         throw new RuntimeException( e );       }     } );      for ( Message message : messages ) {       System.out.println(            "sendDate: " + message.getSentDate()           + " subject:" + message.getSubject() );     }   } } 
like image 75
Dmitriy Dobrotvorskiy Avatar answered Oct 21 '22 19:10

Dmitriy Dobrotvorskiy


Make sure to use the IMAP protocol, as it supports for flagging.

Do following changes to your code:

  1. replace inbox.open( Folder.READ_ONLY ); by inbox.open( Folder.READ_WRITE );
  2. Then after reading the message, set the flag like so:

    message.setFlag(Flags.Flag.SEEN, true); 

Full example:

    import javax.mail.*;     import javax.mail.search.FlagTerm;     import java.util.*;      public class GmailFetch {        public static void main( String[] args ) throws Exception {          Session session = Session.getDefaultInstance(new Properties( ));         Store store = session.getStore("imaps");         store.connect("imap.googlemail.com", 993, "[email protected]", "password");         Folder inbox = store.getFolder( "INBOX" );         inbox.open( Folder.READ_WRITE );          // Fetch unseen messages from inbox folder         Message[] messages = inbox.search(             new FlagTerm(new Flags(Flags.Flag.SEEN), false));          // Sort messages from recent to oldest         Arrays.sort( messages, ( m1, m2 ) -> {           try {             return m2.getSentDate().compareTo( m1.getSentDate() );           } catch ( MessagingException e ) {             throw new RuntimeException( e );           }         } );          for ( Message message : messages ) {           System.out.println(                "sendDate: " + message.getSentDate()               + " subject:" + message.getSubject() );               message.setFlag(Flags.Flag.SEEN, true);         }       }     } 
like image 24
sangamesh Avatar answered Oct 21 '22 19:10

sangamesh