我一直在尝试编写一个定期解析gmail消息内容的应用。我已经阅读了JavaMail常见问题解答,我已经查看了JavaMail下载包中的一些示例,但是无法使其工作。下面的代码当前导致以下gmail错误:
主机未解析:imaps.gmail.com:993
我也试过imap.gmail.com:143但得到:
主机未解析:imap.gmail.com:143
非常感谢任何帮助或建议。 GMailReader是我用来尝试返回gmail imap消息的类:
public class GMailReader extends javax.mail.Authenticator {
private String mailhost = "imaps.gmail.com";
private String user;
private String password;
private Session session;
public GMailReader(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "imaps");
props.setProperty("mail.imaps.host", mailhost);
props.put("mail.imaps.auth", "true");
props.put("mail.imaps.port", "993");
props.put("mail.imaps.socketFactory.port", "993");
props.put("mail.imaps.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.imaps.socketFactory.fallback", "false");
props.setProperty("mail.imaps.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
public synchronized Message[] readMail() throws Exception {
try {
Store store = session.getStore("imaps");
store.connect("imaps.gmail.com", user, password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] msgs = folder.getMessages(1, 10);
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
folder.fetch(msgs, fp);
return msgs;
} catch (Exception e) {
Log.e("readMail", e.getMessage(), e);
return null;
}
}
}
答案 0 :(得分:5)
我找到了一个有用的示例here。我的错误是使用“mail.transport.protocol”而不是“mail.store.protocol。”
答案 1 :(得分:4)
此后是
的更正版本public class GMailReader extends javax.mail.Authenticator {
private static final String TAG = "GMailReader";
private String mailhost = "imap.gmail.com";
private Session session;
private Store store;
public GMailReader(String user, String password) {
Properties props = System.getProperties();
if (props == null){
Log.e(TAG, "Properties are null !!");
}else{
props.setProperty("mail.store.protocol", "imaps");
Log.d(TAG, "Transport: "+props.getProperty("mail.transport.protocol"));
Log.d(TAG, "Store: "+props.getProperty("mail.store.protocol"));
Log.d(TAG, "Host: "+props.getProperty("mail.imap.host"));
Log.d(TAG, "Authentication: "+props.getProperty("mail.imap.auth"));
Log.d(TAG, "Port: "+props.getProperty("mail.imap.port"));
}
try {
session = Session.getDefaultInstance(props, null);
store = session.getStore("imaps");
store.connect(mailhost, user, password);
Log.i(TAG, "Store: "+store.toString());
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized Message[] readMail() throws Exception {
try {
Folder folder = store.getFolder("Inbox");
folder.open(Folder.READ_ONLY);
/* TODO to rework
Message[] msgs = folder.getMessages(1, 10);
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
folder.fetch(msgs, fp);
*/
Message[] msgs = folder.getMessages();
return msgs;
} catch (Exception e) {
Log.e("readMail", e.getMessage(), e);
return null;
}
}
}
再见
答案 2 :(得分:2)
我发现GmailReader概念非常实用且设计精良,符合GmailSender示例所示: Sending Email in Android using JavaMail API without using the default/built-in app
但有任何消息,关于下面提到的错误?并实现了JackN的命题?
最诚挚的问候 SKN
答案 3 :(得分:2)
经过大量的试验,错误和谷歌搜索,这个答案的蛇人版为我提供了gmail阅读器所需的可行示例;
然而,其他人应该知道(如果使用更高版本的Android SDK)Manifest权限要求以及需要使用asyncTask将潜在的长时间运行任务移出主UI线程),这两者都在{ {3}}
我还应该提一下,如果像我一样,你打算也实现一个smtp发送类,我已经在某处讨论过建议应该使用session.getInstance来代替session.getDefaultInstance。