Are there any good short code examples that simply read a new gmail message?(有没有简单的阅读新 gmail 消息的好短代码示例?)
问题描述
我一直在尝试编写一个定期解析 gmail 邮件内容的应用程序.我浏览了 JavaMail 常见问题解答,并且查看了 JavaMail 下载包中的一些示例,但无法使其正常工作.下面的代码目前会导致以下 gmail 错误:
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:
主机未解析:imaps.gmail.com:993
Host is unresolved: imaps.gmail.com:993
我也试过 imap.gmail.com:143 但得到:
I have also tried imap.gmail.com:143 but get:
主机未解析:imap.gmail.com:143
Host is unresolved: imap.gmail.com:143
任何帮助或建议将不胜感激.GMailReader 是我用来尝试返回 gmail imap 消息的类:
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;
}
}
}
推荐答案
我找到了一个例子 这里很有帮助.我的错误是使用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."
这篇关于有没有简单的阅读新 gmail 消息的好短代码示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有简单的阅读新 gmail 消息的好短代码示例?
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
