有没有简单地读取新 Gmail 消息的简单代码示例?
我一直在尝试编写一个定期解析 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;
}
}
}
I have been trying to write an app that periodically parses the contents of gmail messages. I have been through the JavaMail FAQ and I have looked at a number of examples in the JavaMail download package but have been unable to get this to work. The code below currently causes the following gmail error:
Host is unresolved: imaps.gmail.com:993
I have also tried imap.gmail.com:143 but get:
Host is unresolved: imap.gmail.com:143
Any help or advice would be greatly appreciated. GMailReader is the class I am using to try and return gmail imap messages:
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在此处找到了一个有用的示例。我的错误是使用“mail.transport.protocol”而不是“mail.store.protocol”。
I found an example here that was helpful. My error was the use of "mail.transport.protocol" rather than "mail.store.protocol."
修正版本
此后为再见的
hereafter a corrected version of
Bye
我发现 GmailReader 概念非常有用,并且根据此处显示的 GmailSender 示例进行了精心设计:
发送电子邮件在 Android 中使用 JavaMail API 而不使用默认/内置应用程序
但是关于下面询问的错误有任何消息吗?并实施JackN的提议?
此致
SKN
I see that the GmailReader concept very usefull and well designed in accordance whith the GmailSender example showed here:
Sending Email in Android using JavaMail API without using the default/built-in app
But Any news, on the error asked below ? And implementation of the proposition of JackN ?
Best Regards
SkN
经过大量的试验、错误和谷歌搜索,蛇人版本的这个答案提供了我需要的 gmail 阅读器的可行示例;
然而,其他人应该注意(如果使用更高版本的 Android SDK)Manifest 权限要求以及使用 asyncTask 将可能长时间运行的任务移出主 UI 线程的需要,这两者都在 这个 SMTP 示例
我还应该提到的是,如果您像我一样也打算实现 smtp 发送类,我在某处看到过一个讨论,建议应使用 session.getInstance 代替 session.getDefaultInstance。
After a huge amount of trial, error and googling , snakeman's edition of this answer provided the workable example I needed for a gmail reader;
However others should be aware (if using later versions of the Android SDK) of Manifest permission requirements and the need to use asyncTask to move potentially long-running tasks out of the main UI thread), both of which are mentioned in this SMTP example
I should also mention that if, like me, you intend to also implement an smtp sending class, I have seen somewhere a discussion suggesting that session.getInstance should be used in place of session.getDefaultInstance.