4

This is the code that counts the number of mails in the gmail inbox.

Properties props = new Properties();
    props.put("mail.pop3.host" , "pop.gmail.com");
    props.put("mail.pop3.user" , "username");
    props.put("mail.pop3.socketFactory" , 995 );
    props.put("mail.pop3.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
    props.put("mail.pop3.port" , 995);
    Session session = Session.getDefaultInstance(props , new Authenticator() {
        @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication( "username" , "password");
                }
    });
    try {
        Store store  = session.getStore("pop3");
        store.connect("pop.gmail.com" , "username" , "password");
        Folder fldr = store.getFolder("INBOX");
        fldr.open(Folder.HOLDS_MESSAGES);
        int count = fldr.getMessageCount();
        System.out.println(count);
    } catch(Exception exc) {
        System.out.println(exc + " error");
    }

I get the count equal to 7 but i should get 3 because i have only 3 messages in the inbox.

Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
  • 2
    What will happen if you connect with the email program? Do you have any emails in archive folder? I remember when I tried to download my mail with thunderbird it started to download every email I got from the very beginning not looking to what was in the inbox. – Boris Treukhov Jul 24 '11 at 06:51
  • @ Boris Treukhov yes ! Then the count is 7. But i want to calculate in `INBOX` – Suhail Gupta Jul 24 '11 at 06:54

3 Answers3

2

In GMAIL POP3 settings you should enable POP access only for the emails received from the current moment, it's standard GMAIL behavior.

When you enable POP, all messages are downloaded to your client, except for Spam, Trash, and Chats. If you don't want messages that you send from the web interface downloaded to your mail client's inbox, we suggest creating a filter within your client. You may want to contact your mail client's customer service department for instructions on how to categorize downloaded messages.

See the GMAIL troubleshooting article

AFAIK selective sync in GMAIL is only possible with IMAP protocol.

Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91
  • http://stackoverflow.com/questions/953561/check-unread-count-of-gmail-messages-with-python – Boris Treukhov Jul 24 '11 at 07:21
  • of course it's python - I think "Search UNSEEN" exists in any decent IMAP implementation (http://javamail.kenai.com/nonav/javadocs/javax/mail/search/package-summary.html) for example in VB .NET http://www.example-code.com/vbdotnet/imapReadNew.asp - sorry currently I don't have any java sources – Boris Treukhov Jul 24 '11 at 07:51
2

Using IMAP protocol for this.

Properties props = new Properties();
    props.put("mail.imap.host" , "imap.gmail.com");
    props.put("mail.imap.user" , "username");
    props.put("mail.imap.socketFactory" , 993 );
    props.put("mail.imap.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
    props.put("mail.imap.port" , 993);
    Session session = Session.getDefaultInstance(props , new Authenticator() {
        @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication( "username" , "password");
                }
    });
    try {
        Store store  = session.getStore("imap");
        store.connect("imap.gmail.com" , "username" , "password");
        Folder fldr = store.getFolder("Inbox");
        fldr.open(Folder.HOLDS_MESSAGES);
        int count = fldr.getMessageCount();
        System.out.println(count);
    } catch(Exception exc) {
        System.out.println(exc + "error");
    }
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
0

Here is an example of reading emails from inbox.

http://bharatonjava.wordpress.com/2012/09/15/read-emails-form-inbox/

here is the code snippet

public static void main(String[] args){
    Properties props = new Properties();
    try {
        props.load(new FileInputStream(new File("settings.properties")));
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

Session session = Session.getDefaultInstance(props, null);

Store store = session.getStore("imaps");
store.connect("smtp.gmail.com", "yourEmailId@gmail.com",
                    "put your password here");

Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_WRITE); // Folder.READ_ONLY
int messageCount = inbox.getMessageCount();
System.out.println("Total Messages" + messageCount);
}

You have to keep your email settings in a properties file called settings.properties like below.

mail.smtp.host=smtp.gmail.com
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.auth=true
mail.smtp.port=465
Bharat Sharma
  • 141
  • 1
  • 4