前言:
最近有点焦虑,还是坚持自己吧,加油!
和上篇创建文件夹的方法类似 JavaMail创建文件夹 所以,直接切入主题吧。
查看重命名API
public abstract boolean renameTo(Folder newName)
throws MessagingException
Rename this folder; the folder must be closed. If the rename is successfull, a FolderEvent.RENAMED event is sent to all listeners registered with this folder or with the store.
Parameters:
newName - the new name for this folder
Returns:
true if the rename succeeded
Throws:
MessagingException - if there was a problem accessing the store
谷歌翻译如下:重命名此文件夹; 文件夹必须关闭。 如果重命名成功,则将向该文件夹或商店注册的所有侦听器发送Folder Event.RENAMED事件。
参数:该文件夹的新名称
意思很明了吧,也比较简单。
代码实现
public void renameFolder(final String renameFolderName,final String folderName, final createFolderCallBack callBack) throws MessagingException {
Store store = imapSession.getStore("imaps");
store.connect(Constant.imapInfo.getServerName(), Constant.imapInfo.getLoginName(), Constant.imapInfo.getPassword());
Folder folder = store.getDefaultFolder();
try {
//根目录的时候不需要open,不是根目录,就要open
// folder.open(Folder.READ_WRITE);
Folder myFolder = folder.getFolder(renameFolderName);
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);
}
}
});
Folder newFolder = folder.getFolder(folderName);//新建一个文件夹,名字为新的名字,使用myFolder.renameTo实现重命名
boolean isRenamed = myFolder.renameTo(newFolder);
System.out.println("MailHelper rename folder " + isRenamed);
} catch (Exception e) {
Logger.e("MailHelper.class",e.getMessage());
if(null != callBack){
callBack.deleted(folderName);
}
}
}