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 消息的好短代码示例?
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01