0

i wasted atleast 4 hours for researching able to find few things. i have a main menu activity which launches after login screen. i want a gridview, or a listview kind a thing get updated and show inbox mails by listing it and if you click one of them a new activity will launch and show the information like sender, subject, and body of the email on textview's.

this is the one of the source i found: Are there any good short code examples that simply read a new gmail message? tryed both answers including the one provided by OP and the person who answered to the question. both of them isnt worked and idk why?

error message:

05-16 18:35:55.965 1741-1741/app.mailbox E/readMail: Not connected
                                                     java.lang.IllegalStateException: Not connected
                                                         at com.sun.mail.imap.IMAPStore.checkConnected(IMAPStore.java:1992)
                                                         at com.sun.mail.imap.IMAPStore.getFolder(IMAPStore.java:1775)
                                                         at app.mailbox.GMailReader.readMail(GMailReader.java:58)
                                                         at app.mailbox.Menu.onCreate(Menu.java:32)
                                                         at android.app.Activity.performCreate(Activity.java:6679)
                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                         at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                         at android.os.Looper.loop(Looper.java:154)
                                                         at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
05-16 18:35:56.029 1741-1780/app.mailbox E/EGL_emulation: tid 1780: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)

this is the codes after putting few things right:

   package app.mailbox;

import android.util.Log;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

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 = new Properties();
        props.setProperty("mail.store.protocol", "imaps");
        props.setProperty("mail.imaps.host", mailhost);
        props.put("mail.imaps.auth", "true");
        props.put("mail.imaps.port", "993");
        props.put("mail.imaps.socketFactory.port", "993");
        props.put("mail.imaps.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.imaps.socketFactory.fallback", "false");
        props.setProperty("mail.imaps.quitwait", "false");

        if (props == null){
            Log.e(TAG, "Properties are null !!");
        }else{

            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) {
            e.printStackTrace();
        } catch (MessagingException e) {
            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;
        }
    }
}

also do i need to use async task when calling this class from the activity where i want to list mails?

Community
  • 1
  • 1
Airborne
  • 1
  • 2

1 Answers1

0

The problem with copy&paste programming is that you're copying all the errors. Start by fixing these common JavaMail mistakes. You'll find general Gmail instructions in the JavaMail FAQ. I can't tell whether you successfully connected since the GMailReader constructor returns successfully even if the connection fails. You might want to look at the JavaMail debug output.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • yep you're totally right about that. the reason im doing that is im bit short on time. also somewhat i cant find any tutorial or a working example anywhere. gonna keep this updated, thanks again – Airborne May 17 '17 at 21:44
  • There's tons of basic JavaMail examples in the [JavaMail FAQ](https://javaee.github.io/javamail/FAQ) and on the [JavaMail web site](https://javaee.github.io/javamail/#Samples). – Bill Shannon May 17 '17 at 21:48