Pergunta

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.

Foi útil?

Solução

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.

Outras dicas

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");
    }

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top