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

时间:2016-03-21 14:51:28

标签: java javamail

我需要存档/存储java的gmail电子邮件,但我只知道将其设置为已读/未读...

这是我的代码:

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();
      }
   }

有人可以解释我如何存档吗?

1 个答案:

答案 0 :(得分:0)

我假设“存档它”是指“将其移动到服务器上的存档文件夹”。

使用Folder.copyMessages将邮件复制到存档文件夹,然后使用Message.setFlag将原始邮件标记为已删除。