-2

Please help me out with this task. The task is reading the body of the unread emails and mark it as unseen or unread using java. I tried to some extent but whenever I use getContect() method for the reading body it is automatically set to READ. Below I've attached my code for reference and thanks so much for help in advance.

package com;


import java.util.Properties;





import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.UIDFolder;
import javax.mail.search.FlagTerm;

public class ReadMail {
    static String hostName="imap.mail.yahoo.com";
    static String username="user@yahoo.com";
    static String password="password";
    static long maxUid=5;

    /**
     * @param args
     */
    /**
     * @param args
     */
    public static void main(String[] args) {
        Properties sysProps = System.getProperties();

        sysProps.setProperty("mail.store.protocol", "imaps");

        try {

            Session session = Session.getInstance(sysProps, null);

            Store store = session.getStore();

            store.connect(hostName, username, password);

            Folder emailInbox = store.getFolder("INBOX");
            UIDFolder uf = (UIDFolder)emailInbox;

            emailInbox.open(Folder.READ_WRITE);

            int messageCount = emailInbox.getMessageCount();

            System.out.println("Total Message Count: " + messageCount);

            int unreadMsgCount = emailInbox.getUnreadMessageCount();

            System.out.println("Unread Message Count: " + unreadMsgCount);



            Flags seen = new Flags(Flags.Flag.SEEN);
            FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
            Message unreadmsg[] = emailInbox.search(unseenFlagTerm);

            //Message[] message = emailInbox.getMessages(); ///Will get all messages

            // Display message.
            if(unreadmsg.length==0)
                System.out.println("No new  messages found.\n");
            for (int i = 0; i < unreadmsg.length; i++) {
                if(uf.getUID(unreadmsg[i])>maxUid){
                    System.out.println("------------ Message "  + i+ " ------------");

                    System.out.println("SentDate : " + unreadmsg[i].getSentDate());
                    System.out.println("From : " + unreadmsg[i].getFrom());
                    System.out.println("Subject : " + unreadmsg[i].getSubject());
                    maxUid=uf.getUID(unreadmsg[i]);

                    Multipart multipart = (Multipart) unreadmsg[i].getContent();

                    for (int x = 0; x < multipart.getCount(); x++) {
                        BodyPart bodyPart = multipart.getBodyPart(x);

                        String disposition = bodyPart.getDisposition();

                        if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
                            System.out.println("Please find the   attachment : ");

                            DataHandler handler = bodyPart.getDataHandler();
                            System.out.println("Attachment Name: " + handler.getName());
                        } else {
                            System.out.println(bodyPart.getContent());
                        }
                    }
                    System.out.println();

                }


            }



            emailInbox.close(true);

            store.close();

        } catch (Exception mex) {

            mex.printStackTrace();

        }



    }

}
Aamir Khan
  • 101
  • 1
  • 7

1 Answers1

0

The simplest approach is to open the folder READ_ONLY.

If that's not sufficient, you can set the property mail.imaps.peek to true.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40