Android 有什么好的短代码示例可以简单地阅读新的 gmail 邮件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3303805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are there any good short code examples that simply read a new gmail message?
提问by Hymannad
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:
我一直在尝试编写一个定期解析 gmail 邮件内容的应用程序。我已经阅读了 JavaMail 常见问题解答,并且查看了 JavaMail 下载包中的许多示例,但一直无法使其正常工作。下面的代码当前会导致以下 gmail 错误:
Host is unresolved: imaps.gmail.com:993
主机未解析:imaps.gmail.com:993
I have also tried imap.gmail.com:143 but get:
我也试过 imap.gmail.com:143 但得到:
Host is unresolved: imap.gmail.com:143
主机未解析: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:
任何帮助或建议将不胜感激。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;
}
}
}
采纳答案by Hymannad
回答by snakeman
hereafter a corrected version of
此后修正的版本
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;
}
}
}
Bye
再见
回答by Farti Slartbast
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;
经过大量的试验、错误和谷歌搜索,这个答案的蛇人版本提供了我需要的 gmail 阅读器的可行示例;
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
但是其他人应该知道(如果使用更高版本的 Android SDK)Manifest 权限要求以及需要使用 asyncTask 将潜在的长时间运行的任务移出主 UI 线程),这两个都在这个 SMTP 示例中提到
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.
我还应该提到,如果像我一样,你也打算实现一个 smtp 发送类,我在某处看到过一个讨论,建议使用 session.getInstance 代替 session.getDefaultInstance。
回答by snakeman
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
我看到 GmailReader 概念非常有用,并且根据此处显示的 GmailSender 示例进行了精心设计: 使用 JavaMail API 在 Android 中发送电子邮件而不使用默认/内置应用程序
But Any news, on the error asked below ? And implementation of the proposition of HymanN ?
但是关于下面询问的错误有什么消息吗?以及 HymanN 命题的实现?
Best Regards SkN
最好的问候 SkN