【问题标题】:Javamail sustain gmail's "sign out all other sessions"Javamail 支持 gmail 的“退出所有其他会话”
【发布时间】:2013-02-06 03:44:32
【问题描述】:

背景:

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

问题:

我希望我的 javamail 保持登录状态,即使我不小心点击了“退出所有其他会话”按钮。

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

【问题讨论】:

  • 您不需要让您的程序重新登录吗??
  • 好主意。但是,我希望它能够自主工作(无需我的交互)同样注意:如果有任何方法可以检测它是否已被注销,那么我可以采取必要的措施重新登录!
  • 自从我使用JavaMail 以来已经很长时间了,但我想当您尝试下载邮件时它会在某些地方抛出错误。要么这样,要么您可以在每个周期后将自己注销,然后在需要时简单地重新登录...
  • 大多数人只是保存第一次调用 connect 时的用户名和密码,并在 Store 意外关闭时再次调用 connect。我相信 JavaMail 也会从成功的连接中保存用户名和密码,允许您在没有参数的情况下调用 connect()。

标签: java oauth gmail imap jakarta-mail


【解决方案1】:

感谢大家的 cmets/提示。这是java中的快速编写。以防其他人可能需要它。

当然,欢迎任何有关以下代码的 cmets/反馈/评论/潜在问题 :)

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

【讨论】:

    猜你喜欢
    • 2012-05-26
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多