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.
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", "[email protected]", "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;
}
}
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", "[email protected]", "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")
}
}
<uses-permission android:name="android.permission.INTERNET" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With