如何使用 javax mail 存档电子邮件

How do I archive email using javax mail

我需要 archive/store 一个 Gmail 电子邮件表单 java,但我只知道将其设置为 read/unread...

这是我的代码:

public class CheckingMails {

private static Session session = null;
private static Store store = null;
private static Folder inbox = null;

   public static void check(String host, String storeType, final String user,
      final String password) 
   {
      try {
          Properties properties = new Properties();
          properties.setProperty("mail.host", "imap.gmail.com");
          properties.setProperty("mail.port", "995");
          properties.setProperty("mail.transport.protocol", "imaps");
          session = Session.getInstance(properties,new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(user, password);
              }});
          store = session.getStore("imaps");
          store.connect();
          inbox = store.getFolder("INBOX");
          inbox.open(Folder.READ_WRITE);
          Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
          for (int i = 0; i < messages.length; i++) {
              if(messages[i].getSubject().contains("Ticket#")){
                  System.out.println("Number of Ticket = " + messages.length);
                  messages[i].setFlag(Flag.SEEN, true);
              }

          }
          inbox.close(true);
          store.close();

      } catch (NoSuchProviderException e) {
         e.printStackTrace();
      } catch (MessagingException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

谁能告诉我如何存档?

我假设 "archive it" 你的意思是 "move it to the archive folder on the server"。

使用Folder.copyMessages to copy the message to the Archive folder, then mark the original message as DELETED using Message.setFlag