FolderClosed Exception in Javamail

Posted by SikhWarrior on Stack Overflow See other posts from Stack Overflow or by SikhWarrior
Published on 2013-10-23T19:24:12Z Indexed on 2013/10/23 21:54 UTC
Read the original article Hit count: 363

Filed under:
|
|
|
|

Im trying to create a simple mail client in android, and I have the android version of javamail compiling and running in my app.

However, whenever I try to connect and receive mail, I get a Folder Closed exception seen below.

10-23 12:12:13.484: W/System.err(6660): javax.mail.FolderClosedException
10-23 12:12:13.484: W/System.err(6660):     at com.sun.mail.imap.IMAPMessage.getProtocol(IMAPMessage.java:149)
10-23 12:12:13.484: W/System.err(6660):     at com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1262)
10-23 12:12:13.484: W/System.err(6660):     at com.sun.mail.imap.IMAPMessage.getDataHandler(IMAPMessage.java:616)
10-23 12:12:13.484: W/System.err(6660):     at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1398)
10-23 12:12:13.484: W/System.err(6660):     at com.teamzeta.sfu.Util.MailHelper.getMessageHTML(MailHelper.java:60)
10-23 12:12:13.484: W/System.err(6660):     at com.teamzeta.sfu.GetAsyncEmails.onPostExecute(EmailActivity.java:31)
10-23 12:12:13.484: W/System.err(6660):     at com.teamzeta.sfu.GetAsyncEmails.onPostExecute(EmailActivity.java:1)
10-23 12:12:13.484: W/System.err(6660):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-23 12:12:13.484: W/System.err(6660):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-23 12:12:13.484: W/System.err(6660):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-23 12:12:13.484: W/System.err(6660):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 12:12:13.484: W/System.err(6660):     at android.os.Looper.loop(Looper.java:137)
10-23 12:12:13.484: W/System.err(6660):     at android.app.ActivityThread.main(ActivityThread.java:5227)
10-23 12:12:13.484: W/System.err(6660):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 12:12:13.484: W/System.err(6660):     at java.lang.reflect.Method.invoke(Method.java:511)
10-23 12:12:13.484: W/System.err(6660):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-23 12:12:13.484: W/System.err(6660):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-23 12:12:13.494: W/System.err(6660):     at dalvik.system.NativeStart.main(Native Method)

My code is as follows:

public static Message[] getAllMail(String user, String pwd){
        String host = "imap.sfu.ca";

        final Message[] NO_MESSAGES = {};

        Properties properties = System.getProperties();
        properties.setProperty("mail.imap.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.imap.socketFactory.port", "993");

        Session session = Session.getDefaultInstance(properties);

        try {
            Store store = session.getStore("imap");
            store.connect(host, user, pwd);

            Folder folder = store.getFolder("inbox");
            folder.open(Folder.READ_ONLY);

            Message[] messages = folder.getMessages();

            folder.close(true);
            store.close();

            Log.d("####TEAM ZETA DEBUG####", "Content: " + messages.length);

            return messages;

        } catch (NoSuchProviderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.d("####TEAM ZETA DEBUG####", "Returning NO_MESSAGES");
        return NO_MESSAGES;
    }

public static String getMessageHTML(Message message){
    Object msgContent;
    try {
        msgContent = message.getContent();
        if (msgContent instanceof Multipart) {
            Multipart mp = (Multipart) msgContent;
            for (int i = 0; i < mp.getCount(); i++) {
                BodyPart bp = mp.getBodyPart(i);
                if (Pattern
                        .compile(Pattern.quote("text/html"),
                                Pattern.CASE_INSENSITIVE)
                        .matcher(bp.getContentType()).find()) {
                    // found html part
                    return (String) bp.getContent();
                } else {
                    // some other bodypart...
                }
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       

    return "Something went wrong";
}

I couldn't find anything helpful on the web, does anyone have an ideas why this is happening??

This is called in

class GetAsyncEmails extends AsyncTask<String, Integer, Message[]>{

@Override
protected Message[] doInBackground(String... args) {
    // TODO Auto-generated method stub
    Message[] messages = MailHelper.getAllMail(args[0], args[1]);

    return messages;
}

protected void onPostExecute(Message[] result){
    if(result.length > 1){
        Message message = result[0];
        String content = MailHelper.getMessageHTML(message);        
        System.out.println("####TEAM ZETA DEBUG####" + content);
    }
}

}

© Stack Overflow or respective owner

Related posts about java

Related posts about android