我正在创建一个应用,我想在其中阅读所有电子邮件,并希望在列表视图中显示。我一直在寻找,但找不到任何合适的方式。我试过下面的代码:
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();
}
}
但它会返回电子邮件地址,如果我想阅读实际的电子邮件该怎么办?有什么办法吗? Android API会暴露这个吗?还有一件事,我在下面的一个地方找到了接收电子邮件的方法
ContentResolver resolver = getContentResolver();
Uri uriGmail = Uri.parse("content://gmail/");
Cursor cursor = resolver.query(uriGmail, null, null, null, null);
但是游标返回null。我认为没有办法从Android设备上阅读电子邮件。可能是电子邮件被保存在该应用的本地存储(数据库)中(比如Gmail应用)。我进一步探索Android文档,如下所述链接,但找不到方法。 https://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Email.html
提前致谢。
答案 0 :(得分: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();
答案 1 :(得分:0)
答案 2 :(得分:0)
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")
}
}
<uses-permission android:name="android.permission.INTERNET" />