import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
/**
* 移动邮件
* @author lch
*/
public class MoveMail {
public static void main(String argv[]) {
try {
String host = "imap.163.com";//imap服务器
String username = "****@163.com";
String password = "123456";
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imap");
store.connect(host, username, password);
// 获得邮箱内的邮件夹Folder对象,以"读-写"打开
Folder defaultFolder = store.getDefaultFolder();
// Folder[] allFolder = defaultFolder.list(); 获取邮箱中所有文件夹
// for (int j = 0; j < allFolder.length; j++) {
// System.out.println("文件夹" + j + " : " + allFolder[j].getName());
// }
Folder folder = store.getFolder("INBOX");//原件文件夹
folder.open(Folder.READ_WRITE);
Folder dfolder = store.getFolder("草稿箱");//目标文件夹
dfolder.open(Folder.READ_WRITE);
Message[] msgs = folder.getMessages(1, 2);
System.out.println("移动 " + msgs.length + " messages");
if (msgs.length != 0) {
folder.copyMessages(msgs, dfolder);//复制到新文件夹
folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true);//删除源文件夹下的邮件
}
folder.close(true);
store.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}