【问题标题】:Host is unresolved: imap.gmail.com:993主机未解决:imap.gmail.com:993
【发布时间】:2010-08-04 15:32:32
【问题描述】:

当我使用下面的 GMailReader 在 Android 开发环境中尝试 javamail/gmail store.connect 时,有时会收到“主机未解析:imap.gmail.com:993”。为什么有时会失败,而其他时候不会?

public class GMailReader extends javax.mail.Authenticator {
    private String user;
    private String password;
    public GMailReader(String user, String password) {
        this.user = user;
        this.password = password;
    }
    public int getUnreadMessageCount() throws Exception {
        try {
            Properties props = new Properties();
            props.setProperty("mail.store.protocol", "imaps"); 
            Session session = Session.getInstance(props, this); 
            Store store = session.getStore("imaps");
            store.connect("imap.gmail.com", user, password);
            Folder inbox = store.getFolder("Inbox"); 
            inbox.open(Folder.READ_ONLY); 
            int unreadMessageCount = inbox.getUnreadMessageCount(); 
            return unreadMessageCount;
        } catch (Exception e) {
            Log.e("getUnreadMessageCount", e.getMessage(), e);
            return -1;
        }
    }

【问题讨论】:

  • 更新:我在N1上没有看到这个问题,所以可能是开发环境的缺陷,无法在目标机上重现。

标签: android gmail jakarta-mail


【解决方案1】:

我可能打开了太多的 GMailReader 实例,而没有正确关闭它们。自从我将 open 从创建者中移出并添加了 close 方法以来,我已经有一段时间没有看到这个问题了。这种方式似乎运作良好:

public class GMailReader extends javax.mail.Authenticator {
    private String user;
    private String password;
    private Properties properties;
    private Session session; 
    private Store store;
    private Folder inbox; 

    public GMailReader(String user, String password) {
        this.user = user;
        this.password = password;
    }

    public void open() throws Exception {
        try {
            properties = new Properties();
            properties.setProperty("mail.store.protocol", "imaps");
            session = Session.getInstance(properties, this); 
            store = session.getStore("imaps");
            store.connect("imap.gmail.com", user, password);
            inbox = store.getFolder("Inbox"); 
            inbox.open(Folder.READ_ONLY); 
        } catch (Exception e) {
            Log.e(this.getClass().toString(), e.getMessage(), e);
        }
    }
    public void close(boolean expunge) throws Exception {
        try {
            if (inbox.isOpen()) {
                inbox.close(expunge);
            }
            if (store.isConnected()) {
                store.close();
            }
        } catch (Exception e) {
            Log.e(this.getClass().toString(), e.getMessage(), e);
        }
    }
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多