Android 主机未解析:imap.gmail.com:993
当我在Android开发环境中使用下面的gmail阅读器尝试javamail/gmail store.connect时,有时会得到“主机未解析:imap.gmail.com:993”。为什么有时候会失败,而其他的却不会Android 主机未解析:imap.gmail.com:993,android,gmail,jakarta-mail,Android,Gmail,Jakarta Mail,当我在Android开发环境中使用下面的gmail阅读器尝试javamail/gmail store.connect时,有时会得到“主机未解析:imap.gmail.com:993”。为什么有时候会失败,而其他的却不会 public class GMailReader extends javax.mail.Authenticator { private String user; private String password; public GMailReader(Str
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;
}
}
我可能打开了太多Gmail阅读器的实例,但没有正确关闭它们。自从我将open移出creator并添加了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);
}
}
...
我可能打开了太多Gmail阅读器的实例,但没有正确关闭它们。自从我将open移出creator并添加了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);
}
}
...
更新:我没有在N1上看到这个问题,所以可能是开发环境中的缺陷在目标上不可复制。更新:我没有在N1上看到这个问题,所以可能是开发环境中的缺陷在目标上不可复制。