Search code examples
androidarrayscopymessagejakarta-mail

Copying Message Array


I am trying to copy messages from the Inbox folder:

messages = folder.getMessages();
Message[] source = messages;

I am doing this because, when I view messages in my inbox, all recent messages become "seen messages". And the reason for that is the getContent() method. I want to figure out how to copy the messages to another array, and process them all in the source message array.

But when I try the copying process like above, whatever changes I make to the source array also changes in the messages array. I mean if I call getContent() on the source message array, the messages array is effected also.

How can I copy all messages and rupture them completely from the folder?

public Message[] ConnectionToServer(String email, String password)
            throws Exception 
        {
        Properties props = System.getProperties();
        props.setProperty("mail.imaps.partialfetch", "false");
        URLName server = new URLName("imaps://" + email + ":" + password + "@imap.gmail.com/INBOX");
        Session session = Session.getDefaultInstance(props, null);
        folder = session.getFolder(server);

        if (folder == null) 
        {
            System.exit(0);
        }
        folder.open(Folder.READ_WRITE);     
        messages = folder.getMessages();

        for (int i = messages.length - 1; i >= 23; i--) 
        {
            Part p = messages[i]; 

            subject = messages[i].getSubject();

            if (messages[i].isSet(Flags.Flag.RECENT)) {
            isSet = true;
            System.out.println("Recent");
            isSetlist.add(String.valueOf(isSet));
        }
        if (messages[i].isSet(Flags.Flag.SEEN)) 
        {
            isSet = false;
            System.out.println("Read");
            isSetlist.add(String.valueOf(isSet));
        }
        else
        {
            isSet = true;
            System.out.println("Recent");
            isSetlist.add(String.valueOf(isSet));
        }

            body = getText(p);

            list.add(body);
        }
        return (Message[]) messages;
    }

and the getContent method is in my getText() method:

public String getText(Part p) throws MessagingException, IOException {

        if (p.isMimeType("text/*")) {
            boolean textIsHtml = false;
            String s = (String) p.getContent();

            textIsHtml = p.isMimeType("text/html");
            return String.valueOf(s);
        }

        if (p.isMimeType("multipart/alternative")) {
            // prefer html text over plain text
            Multipart mp = (Multipart) p.getContent();
            String text = null;
            for (int i = 0; i < mp.getCount(); i++) {
                Part bp = mp.getBodyPart(i);
                if (bp.isMimeType("text/plain")) {
                    if (text == null)
                        text = getText(bp);
                    continue;
                } else if (bp.isMimeType("text/html")) {
                    String s = getText(bp);
                    if (s != null)
                        return String.valueOf(s);
                } else {
                    return getText(bp);
                }
            }
            return text;
        } else if (p.isMimeType("multipart/*")) {
            Multipart mp = (Multipart) p.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                String s = getText(mp.getBodyPart(i));
                if (s != null)
                    return String.valueOf(s);
            }
        }

        return null;
    }

Solution

  • You're copying the pointer to the array, you're not copying the message content. You're two levels of indirection away from what you want to do.

    But, copying the message content isn't going to solve your problem. The process of copying the message content is going to set the SEEN flag. What you need to do is make a copy of all the SEEN flags before you access the message content.

    Or, you can use the com.sun.mail.imap.IMAPMessage.setPeek() method to cause accesses of the message content to NOT set the SEEN flag. Cast the Message object to IMAPMessage to use this method.