我一直在试图写一个应用程序,定期分析Gmail邮件的内容。我已通过JavaMail的常见问题,我已经看过一些在JavaMail的下载包中的例子,但一直无法得到这个工作。低于目前的code使下面的Gmail错误:
主持人是没有得到解决:imaps.gmail.com:993
我也尝试imap.gmail.com:143但得到的:
主持人是没有得到解决:imap.gmail.com:143
任何帮助或建议将不胜AP preciated。 GMailReader是我使用的尝试,并返回Gmail的IMAP邮件类:
公共类GMailReader扩展javax.mail.Authenticator {
私人字符串的邮件主机=imaps.gmail.com;
私人字符串的用户;
私人字符串密码;
私人届会议;
公共GMailReader(用户字符串,字符串密码){
this.user =用户;
this.password =密码;
属性道具=新特性();
props.setProperty(mail.transport.protocol,IMAPS);
props.setProperty(mail.imaps.host,邮件主机);
props.put(mail.imaps.auth,真);
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,假);
props.setProperty(mail.imaps.quitwait,假);
会议=作为Session.getDefaultInstance(道具,这一点);
}
公共同步信息[] readMail()抛出异常{
尝试 {
店中店= session.getStore(IMAPS);
store.connect(imaps.gmail.com,用户名,密码);
文件夹文件夹= store.getFolder(收件箱);
folder.open(Folder.READ_ONLY);
消息[]封邮件= folder.getMessages(1,10);
FetchProfile FP =新FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
folder.fetch(消息,FP);
返回封邮件;
}赶上(例外五){
Log.e(readMail,e.getMessage(),E);
返回null;
}
}
}
解决方案
我找到了一个例子here这是有帮助的。我的错误是使用mail.transport.protocol,而不是mail.store.protocol。
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;
}
}
}
解决方案
I found an example here that was helpful. My error was the use of "mail.transport.protocol" rather than "mail.store.protocol."