3

I am looking for a way to programmatically intercept incoming emails on Android, no matter their source (gmail, exchange, IMAP, etc.). My main concern is to have a look at the mail headers rather than the mail body. It can be done for GMail only using the following code -

ContentResolver cr = getContentResolver(); 
Cursor unread = cr.query(Uri.parse("content://gmail-ls/conversations/xxxxxx@gmail.com"), null, "label:^u", null, null); 
unread.moveToFirst(); 
int subjectIdx = unread.getColumnIndex("subject");

do {
  String subject = unread.getString(subjectIdx); 
} while (unread.moveToNext());

... there is also this answer. Is something like this available to non-gmail accounts? I know that theoretically I can ask the user for the username and password and connect via imap, but I can't do it from the business aspect.

Any solution that is possible will be welcome, i.e. Java SDK, NDK, undocumented API or whatever means necessary.

Community
  • 1
  • 1
David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125
  • 1
    The problem that you face is that there is no guarantee that the user will be using the default mail client. I use K9 mail, and this uses a its own database and is completely separate from the stock mail client, so the approach that you're looking at will miss all emails which are retrieved by K9, in my case. – Mark Allison Jul 27 '11 at 10:45
  • @Mark - I know, but we start with the default one – David Rabinowitz Jul 27 '11 at 10:50
  • @Mark - also, we know how to do this with K9 – David Rabinowitz Jul 27 '11 at 12:12
  • I was only using K9 as an example. To cover every possibility, you'll need to look at every possible email client available through the Android Market and other sources, and devise a method for handling each. That's a lot of work, and a lot of complexity. – Mark Allison Jul 27 '11 at 12:18
  • @Mark - you are correct, but it's a question of ROI. K9 for example is (VERY roughly) 10 times more popular than MailDroid for example. Overtime we will earn what our users are using and will adapt to it. – David Rabinowitz Jul 27 '11 at 12:45

1 Answers1

0

Answering my own question:

Google has released push notifications for the GMail Rest API. I have not used it, but the documentation is here

Old answer

There are two options to do this:

  • Use the GMail Inbox Feed - it's an atom feed whom you can get access to using OAuth. It shows only unread items in the inbox, so it is very limited.
  • Use the GMail IMAP extensions, and connect using IMAP and OAuth. Here you have access to the entire user mailbox.

Both of this methods has the limit of being pull based rather than push based. I suspect (even though I haven't tried it) that the IMAP push wouldn't work, and in any case I wouldn't trust it on mobile device.

David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125