Thursday, 19 September 2013

How can I read inbox gmail messages from my gmail account?

How can I read inbox gmail messages from my gmail account?

How should I load the body of emails from Gmail in my listView without an
internet connection?
I use two classes to read messages from my gmail account.
public class GMailReader extends javax.mail.Authenticator {
private static final String TAG = "GMailReader";
private String mailhost = "imap.gmail.com";
private Session session;
private Store store;
public GMailReader(String user, String password) {
Properties props = System.getProperties();
if (props == null){
Log.e(TAG, "Properties are null !!");
}else{
props.setProperty("mail.store.protocol", "imaps");
Log.d(TAG, "Transport:
"+props.getProperty("mail.transport.protocol"));
Log.d(TAG, "Store: "+props.getProperty("mail.store.protocol"));
Log.d(TAG, "Host: "+props.getProperty("mail.imap.host"));
Log.d(TAG, "Authentication: "+props.getProperty("mail.imap.auth"));
Log.d(TAG, "Port: "+props.getProperty("mail.imap.port"));
}
try {
session = Session.getDefaultInstance(props, null);
store = session.getStore("imaps");
store.connect(mailhost, user, password);
Log.i(TAG, "Store: "+store.toString());
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized Message[] readMail() throws Exception {
try {
Folder folder = store.getFolder("Inbox");
folder.open(Folder.READ_ONLY);
Message[] msgs = folder.getMessages();
return msgs;
} catch (Exception e) {
Log.e("readMail", e.getMessage(), e);
return null;
}
}
}
and MainClass
public class MainActivity extends Activity {
MyTask mt;
Message[] msg = null;
GMailReader gm=null;
ListView lv;
ArrayList<String> catnames = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gm=new GMailReader("myemail@gmail.com","mypassword");
mt = new MyTask();
mt.execute(gm);
lv = (ListView)findViewById(R.id.listView1);
}
class MyTask extends AsyncTask<GMailReader, Integer, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(GMailReader... urls) {
try {
msg = gm.readMail();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
for(int i=0; i<msg.length; i++)
{
catnames.add(msg.toString());
}
adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, catnames);
lv.setAdapter(adapter);
}
}
}
My Manifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jim"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission
android:name="com.google.android.gm.permission.READ_CONTENT_PROVIDER"
/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.jim.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
My activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
I tested this on Android 4.1.2. But it don't work. Why? Maybe there's
another solution?

No comments:

Post a Comment