javamail android fecthing电子邮件?

时间:2017-05-17 15:48:38

标签: android email javamail fetch

我浪费了至少4个小时进行研究才能找到一些东西。我有一个主菜单活动,在登录屏幕后启动。我想要一个gridview,或者listview类的东西得到更新,并通过列出它显示收件箱邮件,如果你点击其中一个新活动将启动并在textview&#39上显示电子邮件的发件人,主题和正文等信息; S

这是我找到的来源之一:Are there any good short code examples that simply read a new gmail message?  尝试了两个答案,包括OP提供的答案和回答问题的人。他们俩都没有工作,为什么这么说?

错误消息:

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)

这是在放好几件事之后的代码:

   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;
        }
    }
}

从我要列出邮件的活动中调用此类时,我还需要使用异步任务吗?

1 个答案:

答案 0 :(得分:0)

复制和粘贴编程的问题在于您正在复制所有错误。首先修复这些common JavaMail mistakes。您可以在JavaMail FAQ中找到常规Gmail说明。我无法判断您是否成功连接,因为即使连接失败,GMailReader构造函数也会成功返回。您可能需要查看JavaMail debug output