3

I am creating an app, in which I want to read all emails and want to display in a List View. I have been searching, but could not find any suitable way. I have tried below code:

private static final String[] PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
};

ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,         PROJECTION, null, null, null);
if (cursor != null) {
try {
    final int contactIdIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID);
    final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
    long contactId;
    String displayName, address;
    while (cursor.moveToNext()) {
        contactId = cursor.getLong(contactIdIndex);
        displayName = cursor.getString(displayNameIndex);
        address = cursor.getString(emailIndex);

    }
} finally {
    cursor.close();
    }
}

but it return email address, what if I want to read actual emails? Is there any way? Does Android API expose this? One more thing, in one place I found below approach to get emails as

ContentResolver resolver = getContentResolver();
Uri uriGmail = Uri.parse("content://gmail/");
Cursor cursor = resolver.query(uriGmail, null, null, null, null);

But cursor returns null. What I believe there is no way to read emails from Android device. May be emails get saved in local storage(data base) of that app(let say Gmail app). I further explore Android documentation as mentioned below link but could not find the way. https://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Email.html

Thanks in advance.

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58

3 Answers3

2

Try this,

Download mail.jar https://code.google.com/archive/p/javamail-android/downloads

    new MyAsynk().execute();

    public class MyAsynk extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            Properties props = new Properties();
            props.setProperty("mail.store.protocol", "imaps");
            try {
                Session session = Session.getInstance(props, null);
                Store store = session.getStore();
                store.connect("imap.gmail.com", "youremail@gmail.com", "password");
                Folder inbox = store.getFolder("INBOX");
                inbox.open(Folder.READ_ONLY);
                javax.mail.Message msg = inbox.getMessage(inbox.getMessageCount());
                javax.mail.Address[] in = msg.getFrom();
                for (javax.mail.Address address : in) {
                    System.out.println("FROM:" + address.toString());
                }
                Multipart mp = (Multipart) msg.getContent();
                BodyPart bp = mp.getBodyPart(0);
                System.out.println("SENT DATE:" + msg.getSentDate());
                System.out.println("SUBJECT:" + msg.getSubject());
                System.out.println("CONTENT:" + bp.getContent());
            } catch (Exception mex) {
                mex.printStackTrace();
            }
            return null;
        }
    }
Komal12
  • 3,340
  • 4
  • 16
  • 25
1
GlobalScope.launch {
        val props = Properties()
        props.setProperty("mail.store.protocol", "imaps")
        try{
            val session = Session.getInstance(props, null)
            val store = session.store
            store.connect("imap.gmail.com", "youremail@gmail.com", "password")
            val inbox = store.getFolder("INBOX")
            inbox.open(Folder.READ_ONLY)
            Log.d("MyLog", inbox.messageCount.toString())
            val msg = inbox.getMessage(inbox.messageCount)
            val address = msg.from
            for (adr in address) {
                Log.d("MyLog", adr.toString())
            }
            val mp = msg.content as Multipart
            val bp = mp.getBodyPart(0)
            Log.d("MyLog", bp.content.toString())
        }catch (e: Exception){
            Log.d("MyLog", "Error $e")
        }
    }
  1. Download jar files: additionnal.jar, mail.jar, activation.jar
  2. Add those jars in External Libraries in android studio
  3. Add internet permission in manifest file:

<uses-permission android:name="android.permission.INTERNET" />

  1. Enable less secure apps to access your Gmail
Alex Orlov
  • 11
  • 2
0
 Properties props = new Properties();
    //IMAPS protocol
    props.setProperty(“mail.store.protocol”, “imaps”);
    //Set host address
    props.setProperty(“mail.imaps.host”, imaps.gmail.com);
    //Set specified port
    props.setProperty(“mail.imaps.port”, “993″);
    //Using SSL
    props.setProperty(“mail.imaps.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”);
    props.setProperty(“mail.imaps.socketFactory.fallback”, “false”);
    //Setting IMAP session
    Session imapSession = Session.getInstance(props);

Store store = imapSession.getStore(“imaps”);
//Connect to server by sending username and password.
//Example mailServer = imap.gmail.com, username = abc, password = abc
store.connect(mailServer, account.username, account.password);
//Get all mails in Inbox Forlder
inbox = store.getFolder(“Inbox”);
inbox.open(Folder.READ_ONLY);
//Return result to array of message
Message[] result = inbox.getMessages();
faiyaz meghreji
  • 271
  • 1
  • 2
  • 9
  • I have already tried that as well but android studio is not recognizing classes like Session and store.My question is are those available in some kind of specific libraries or those are android sdk API? – Abdul Waheed Feb 27 '17 at 05:53