Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

android - Are there any good short code examples that simply read a new gmail message?

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; 
        } 
    } 
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found an example here that was helpful. My error was the use of "mail.transport.protocol" rather than "mail.store.protocol."


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...