Javamail支持gmail“退出所有其他会话”

时间:2013-02-06 03:44:32

标签: java oauth gmail imap javamail

背景

我正在尝试监控我的Gmail收件箱中的某些类型的电子邮件并采取行动。我已成功设置我的帐户以使用javamail的addMessageCountListener.messagesAdded()来监控我的收件箱以收听新邮件,并且我正在闲置线程,直到收到新邮件。

问题:

即使在我意外点击“退出所有其他会话”按钮后,我希望我的javamail仍然保持登录状态。

Sign out all other sessions

我知道这是可能的,因为我的手机gmail会话(使用gmail的本机应用程序)对此具有弹性。

1 个答案:

答案 0 :(得分:0)

谢谢大家的意见/提示。这是java中的快速写作。如果其他人可能需要它。

当然,我们非常欢迎以下代码提出任何意见/反馈/评论/潜在问题:)

package com.anand.test;

import java.util.Properties;

import javax.mail.FolderClosedException;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.event.ConnectionEvent;
import javax.mail.event.ConnectionListener;
import javax.mail.event.MessageCountEvent;
import javax.mail.event.MessageCountListener;

import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPSSLStore;

class GMailStayConnect {
    public static void main(String a[]) {
        MailConnector m = new MailConnector("uname", "pwd");
        Thread newThrd = new Thread(m);
        newThrd.start();
    }
}

class StoreGetter {
    public static IMAPSSLStore getGStore(String uname, String passw){
        Properties props = new Properties();
        props.put("mail.imaps.sasl.enable", "true");
        props.put("mail.imaps.sasl.mechanisms", "XOAUTH");
        props.put("mail.debug", "true");

        Session session = Session.getInstance(props);
        session.setDebug(true);

        IMAPSSLStore store = new IMAPSSLStore(session, null);
        try {
            store.connect(uname, passw);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return store;
    }
}

class MailConnector implements Runnable {
    private String uname = "";
    private String passw = "";

    public MailConnector(String uname, String passw) {
        this.uname = uname;
        this.passw = passw;
    }

    public void run() {
        IMAPSSLStore store = StoreGetter.getGStore(uname, passw);
        try {
            IMAPFolder inbox = (IMAPFolder) store.getFolder("Inbox");
            inbox.addMessageCountListener(new MessageCountListener() {
                public void messagesAdded(MessageCountEvent e) {
                    // My custom action goes here on e.getMessages()
                }

                public void messagesRemoved(MessageCountEvent e) {
                    // My custom action goes here on e.getMessages()
                }
            });
            inbox.addConnectionListener(new ConnectionListener() {
                public void opened(ConnectionEvent e) {
                    // System.out.println("Opened !!");
                }

                public void disconnected(ConnectionEvent e) {
                    // System.out.println("Disconnected !!");
                }

                public void closed(ConnectionEvent e) {
                    // System.out.println("Closed !!");
                    // Another place to handle reconnecting
                }
            });
            while (true) {
                inbox.idle();
            }
        } catch (FolderClosedException e1) {
            // Place to handle reconnecting
            GMailStayConnect.main(null);
        } catch (MessagingException e1) {
            e1.printStackTrace();
        }
    }
}