Java Email Folder copy email

Description

Java Email Folder copy email

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

public class Main {
   public static void main(String[] args) throws Exception {
      String host = "pop.gmail.com", username = "usr.some", password = "some.usr";

      Properties props = new Properties();
      Session session = Session.getInstance(props);
      session.setDebug(true);//from   w  w w .j a v  a2  s  . com
      Store store = session.getStore("imaps");

      store.connect(host, username, password);
      Folder inbox = store.getFolder("Inbox");

      Folder defaultFolder = store.getDefaultFolder();
      Folder backup = defaultFolder.getFolder("backup");
      boolean isCreated = backup.create(Folder.HOLDS_MESSAGES);
      System.out.println("created: " + isCreated);

      inbox.open(Folder.READ_ONLY);
      Message[] emails = inbox.getMessages();
      inbox.copyMessages(emails, backup);
   }
}



PreviousNext

Related