JavaMail删除文件夹

前言

有时候会有些迷茫,就慢慢坚持吧,时间会证明一切吧!


JavaMail创建文件夹  JavaMail重命名文件夹

删除文件夹和重命名,创建一样,FolderListener监听分别为创建,重命名和删除的成功回调,其实可以写到一篇博客里,我想既然分开了,就分开写吧,单一职责,嘿嘿!

 

查看API文档

 

public abstract boolean delete(boolean recurse)
                        throws MessagingException
Delete this folder and possibly any subfolders. This operation can only be performed on a closed folder. If recurse is true, then all subfolders are deleted first, then any messages in this folder are removed and it is finally deleted; FolderEvent.DELETED events are sent as appropriate. If recurse is false, then the behaviour depends on the folder type and store implementation as followd:
If the folder can only conrain messages, then all messages are removed and then the folder is deleted; a FolderEvent.DELETED event is sent.
If the folder can onlu contain subfolders, then if it is empty it will be deleted and a FolderEvent.DELETED event is sent; if the folder is not empty then the delete fails and this method returns false.
If the folder can contain both subfolders and messages, then if the folder does not contain any subfolders, any messages are deleted, the folder itself is deleted and a FolderEvent.DELETED event is sent; if the folder does contain subfolders then the implementation may choose from the following three behaviors:
it may return false indicting the operation failed
it may remove all messages within the folder, send a FolderEvent.DELETED event, and then return true to indicate the delete was performed. Note this does not delete the folder itself and the exists() operation for this folder will return true
it may remove all messages within the folder as per the previous option; in addition it may change the type of the Folder to only HOLDS_FOLDERS indictaing that messages may no longer be added
FolderEvents are sent to all listeners registered with this folder or with the Store.
Parameters:
recurse - whether subfolders should be recursively deleted as well
Returns:
true if the delete operation succeeds
Throws:
MessagingException - if there was a problem accessing the store

 翻译:上述意思可以这样理解,如果自己定义的文件夹下有邮件,则可能删除不了,需要将其内邮件移出到其他文件夹,再进行删除操作;

 如果其下有文件夹,子文件夹中没有邮件,则执行递归删除,上述的情况比较多,大家可以多尝试下,可以简单理解为,如果有邮件的,则删除不了,没有邮件,则可以删除,我这边就是这样简单处理的。

实现代码

 public void deleteFolder(final String folderName, final createFolderCallBack callBack) throws MessagingException {
        Store store = imapSession.getStore("imaps");
        store.connect(Constant.imapInfo.getServerName(), Constant.imapInfo.getLoginName(), Constant.imapInfo.getPassword());
        final Folder folder = store.getDefaultFolder();
        try {
            //根目录的时候不需要open,不是根目录,就要open
            // folder.open(Folder.READ_WRITE);
            Folder myFolder = folder.getFolder(folderName);
            myFolder.addFolderListener(new FolderListener() {
                @Override
                public void folderRenamed(FolderEvent folderEvent) {
                    if(null != callBack){
                        callBack.renamed(folderName);
                    }
                }
                @Override
                public void folderDeleted(FolderEvent folderEvent) {
                    if(null != callBack){
                        callBack.deleted(folderName);
                    }
                }
                @Override
                public void folderCreated(FolderEvent folderEvent) {
                    if(null != callBack){
                        callBack.created(folderName);
                    }
                }
            });
            //delete recurse 参数含义: 子文件夹是否应该递归删除
            //如果其下有邮件,则删除失败,提示用户
            //如果没有邮件,则可以删除成功
            boolean isDelete = myFolder.delete(true);
            System.out.println("MailHelper delete folder " + isDelete);
        } catch (Exception e) {
            Logger.e("MailHelper.class",e.getMessage());
            if(null != callBack){
                callBack.deleted(folderName);
            }
        }
    }


发布了60 篇原创文章 · 获赞 109 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/shenshibaoma/article/details/72841597
今日推荐