JavaMail reads recent unread emails using IMAP

I have a requirement to receive unread emails from Gmail. I am using the Java Mail API. By default, this API receives messages from the oldest to the latest. But first I need to get the latest letters. Is it possible? Thanks in advance.

+11
source share
4 answers

Here is an example. Remember to add javax.mail to 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, " username@gmail.com ", "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() ); } } } 
+22
source

JavaMail provides you with an array of Message objects. Messages are in the received order. If you want to view the most recently received messages first, go through the array in the reverse order. If you want to first view the last messages sent , you will need to sort the array as described in another answer.

+2
source

Make sure you are using IMAP, as it supports labeling.

Make the following changes to your code:

  1. replace inbox.open( Folder.READ_ONLY ); from inbox.open( Folder.READ_WRITE );
  2. Then, after reading the message, set the flag as follows:

     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, " username@gmail.com ", "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); } } } 
+1
source

I think this can help access read / unread / recent emails and change your variables to suit your needs.

 // search for all "unseen" messages Flags seen = new Flags(Flags.Flag.SEEN);// try changing this SEEN to RECENT // set it true or false for seen & unseen mail FlagTerm unseenFlagTerm = new FlagTerm(seen, false) Message messages[] = inbox.search(unseenFlagTerm); 
0
source

Source: https://habr.com/ru/post/982924/


All Articles