有没有简单的阅读新 gmail 消息的好短代码示例? [英] Are there any good short code examples that simply read a new gmail message?

查看:13
本文介绍了有没有简单的阅读新 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 消息的好短代码示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆