8

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; 
        } 
    } 
}
jacknad
  • 13,483
  • 40
  • 124
  • 194

4 Answers4

5

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

Community
  • 1
  • 1
jacknad
  • 13,483
  • 40
  • 124
  • 194
  • Host is unresolved: imaps.gmail.com. How can I solve this – Shahinoor Shahin Sep 02 '16 at 08:49
  • This is unrelated to the original question. Open a new question that includes your code, what works, what doesn't work, the error message, etc. As you are drafting the message you may find it has already been answered in the related answers that pop up. – jacknad Sep 02 '16 at 18:40
4

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

snakeman
  • 326
  • 2
  • 3
  • 8
2

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;

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

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.

Community
  • 1
  • 1
2

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

But Any news, on the error asked below ? And implementation of the proposition of JackN ?

Best Regards SkN

Community
  • 1
  • 1
snakeman
  • 326
  • 2
  • 3
  • 8
  • Apparently according javamail doc: http://www.oracle.com/technetwork/java/faq-135477.html#gmail To connect to Gmail using the IMAP protocol instead of the POP3 protocol, simply change the host name "pop.gmail.com" to "imap.gmail.com" and change the protocol name "pop3s" to "imaps" in the above instructions. – snakeman Oct 25 '11 at 16:23