android – 是否有任何好的短代码示例只是读取一个新的Gmail邮件?

前端之家收集整理的这篇文章主要介绍了android – 是否有任何好的短代码示例只是读取一个新的Gmail邮件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在尝试编写一个定期解析gmail消息内容的应用程序.我已经阅读了 JavaMail常见问题解答,我已经查看了JavaMail下载包中的一些示例,但无法使其工作.下面的代码当前导致以下gmail错误

Host is unresolved: imaps.gmail.com:993

我也试过imap.gmail.com:143但得到:

Host is unresolved: 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; 
        } 
    } 
}

解决方法

我找到了一个有用的示例 here.我的错误是使用“mail.transport.protocol”而不是“mail.store.protocol”.

猜你在找的Android相关文章