[英]How to read emails programmatically in android?
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? Android API 会公开这个吗? 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.我认为无法从 Android 设备读取电子邮件。 May be emails get saved in local storage(data base) of that app(let say Gmail app).可能是电子邮件被保存在该应用程序的本地存储(数据库)中(比如 Gmail 应用程序)。 I further explore Android documentation as mentioned below link but could not find the way.我进一步探索了 Android 文档,如下所述链接,但找不到方法。 https://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Email.html 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下载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;
}
}
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();
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" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.