0

I have do an android application, and now, this application needs to read emails. I found this code:

      protected ArrayList<Mail> doInBackground(Void... params) {
    ArrayList<Mail> mails = new ArrayList<Mail>(32);
        boolean finish = false;

        try {
            String direcCompleta = URI_PREFIX + email;
            Uri a = Uri.parse(direcCompleta);
            Cursor  cCursor = resolver.query(a, null, null , null, null);

            while (cCursor.moveToNext() && (! finish)) {
                finish = fromTime.before(new Date(cCursor.getLong(1)));

                if (! finish){
                    String conv_id  = cCursor.getString(cCursor.getColumnIndex("_id"));
                    Uri    uri      = Uri.parse(URI_PREFIX + email + "/" + conv_id + "/messages");
                    Cursor mCursor  = resolver.query(uri, MESSAGE_PROJECTION, null, null, null);                    

                    while (mCursor.moveToNext() &&  (! finish)){
                        long mtime = mCursor.getLong(4);
                        finish = fromTime.before(new Date(mtime));
                        if (! finish){
                            mails.add(new Mail(mCursor.getString(0), mCursor.getString(2), mCursor.getString(1), mCursor.getString(4), mtime));                             
                        }
                    }                       
                }               
            }
        } catch (Exception ex){  
            Log.e("GmailReadApp", ex.toString());
            mails.add(new Mail(null, null, ex.toString(), ex.toString(), 0));
        }

        return mails;
    }

But cCursor is null. I have my mail account in app: "Email", because i work with android sdk, and it doesn´t have app: "GMAIL". And android sdk doesn´t have market.

Somebody can help me please? thanks. (no matter if it's for gmail, yahoo, hotmail,....)

Nobelisco
  • 351
  • 1
  • 2
  • 6

2 Answers2

0

I have do an android application, and now, this application needs to read emails

Write your own email client from scratch, then. There is an Android port of JavaMail floating around somewhere. Or, grab an existing open source email client (e.g., K9) and modify it to suit.

I found this code

There is no possible value of URI_PREFIX that is documented and supported for use on Android. Even for the undocumented and unsupported values, you cannot hold the permissions necessary to read those messages.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You can do it with Kotlin.

  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" />
  4. Enable less secure apps to access your Gmail

Add this code to your app:

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")
            }
        }
Alex Orlov
  • 11
  • 2