读取未阅读的邮件,并使用Java将其标记为看不见的



请帮助我完成此任务。任务是阅读未读电子邮件的主体,并使用Java将其标记为看不见的或未读的。我尝试在某种程度上尝试,但是每当我使用getContect((方法作为阅读主体时,它会自动设置为读取。下面我附上了我的代码以供参考,并非常感谢提前的帮助。

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

    }
}

最简单的方法是打开文件夹read_only。

如果这还不够,则可以将属性邮件设置为true。

相关内容

  • 没有找到相关文章

最新更新