删除邮件

public void deleteEmail() throws Exception {



        // 连接并打开邮箱

        Store store = session.getStore();
        try {
            store.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 定位收件箱
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_WRITE);

        if (folder instanceof POP3Folder) {
            POP3Folder inbox = (POP3Folder) folder;
            Message[] messages = inbox.getMessages();
            for (int i = 0; i < messages.length; i++) {
                MimeMessage mimeMessage = (MimeMessage) messages[i];
                String uid = inbox.getUID(mimeMessage);

                // 判断数据库是否已经接受过当前邮件

                if (null != ) {
                    mimeMessage.setFlag(Flags.Flag.DELETED, true);
                }

            }
        } else if (folder instanceof IMAPFolder) {
            IMAPFolder inbox = (IMAPFolder) folder;
            Message[] messages = inbox.getMessages();
            for (int i = 0; i < messages.length; i++) {
                MimeMessage mimeMessage = (MimeMessage) messages[i];
                String uid = Long.toString(inbox.getUID(mimeMessage));

                // 判断数据库是否已经接受过当前邮件

                if (null != ) {
                    mimeMessage.setFlag(Flags.Flag.DELETED, true);
                }
            }
        }
        folder.close(true);
        store.close();
    }
 

你可能感兴趣的:(java,mail)