在Android应用程序上多次从gmail检索所有电子邮件

时间:2013-03-22 15:20:57

标签: java android gmail-imap

这可能是愚蠢的问题,但...... ....

以下代码可以完美地从gmail中检索所有电子邮件。但是,在运行应用程序一次后,gmail在其“转发和POP / IMAP”设置中设置“状态:为自EXECUTION_APP_TIME以来已到达的所有邮件启用POP”,并且应用程序停止获取旧消息(这是有道理的)。 / p>

每次执行应用程序时,我都需要从gmail获取所有消息,但它只能运行一次。我需要在每次执行后将“转发和POP / IMAP”中的配置更改为“为所有邮件启用POP(即使已经下载的邮件)”。

有谁知道我怎么能解决它?   有没有办法通过代码自动设置它。

以下是源代码:

public void connect(){
    Properties props = new Properties();
    props.put("mail.pop3.host" , "pop.gmail.com");
    props.put("mail.pop3.user" , username);
    // Start SSL connection
    props.put("mail.pop3.socketFactory" , 995 );
    props.put("mail.pop3.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
    props.put("mail.pop3.port" , 995);

    Log.e("asdasd", "teste");

    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", 995 , username , password);
        Folder fldr = store.getFolder("Inbox");
        fldr.open(Folder.HOLDS_MESSAGES);

        int totalMsg = fldr.getMessageCount();
        int unread = fldr.getUnreadMessageCount();
        System.out.println("Total MSG: " + totalMsg + " unreaed: " + unread  );

        Message[] messages = fldr.getMessages();
        //Message[] msg = fldr.search(new FlagTerm(new Flags(Flag.SEEN), false));
        Message[] msg = messages;

        System.out.print("size " + messages .length);
        if (messages.length == 0) System.out.println("No messages found.");

        for (int i = 0; i < messages.length; i++) {

            System.out.println("Subject : " + messages[i].getSubject());

            if(messages[i].getSubject().equals("test223")){
                System.out.println("Message " + (i + 1));
                System.out.println("From : " + messages[i].getFrom()[0]);
                System.out.println("Subject : " + messages[i].getSubject());
                Object content = messages[i].getContent();

                if (content instanceof String)  
                    System.out.print((String)content);  
                /* text/plain = String
                 * multipart" = Multipart
                 * MimeMessage
                 * input stream = Unknown Data Handler 
                 */                 

                Multipart multipart = (Multipart) msg[i].getContent();

                for (int x = 0; x < multipart.getCount()-1; x++) {
                    BodyPart bodyPart = multipart.getBodyPart(x);

                    String disposition = bodyPart.getDisposition();

                    if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
                        System.out.println("Mail have some attachment : ");

                        DataHandler handler = bodyPart.getDataHandler();
                        System.out.println("file name : " + handler.getName());
                    } else {
                        System.out.println(bodyPart.getContent());
                    }
                }
            }
        }

        fldr.close(true);
        store.close();


    } catch(Exception exc) {
        System.out.println(exc + " error");
    }       
}

0 个答案:

没有答案