Make sure you are using IMAP, as it supports labeling.
Make the following changes to your code:
- replace
inbox.open( Folder.READ_ONLY ); from inbox.open( Folder.READ_WRITE ); 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 );
source share