Advertisement
Zebr911

IMAP quota and folder size

May 9th, 2012
1,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.59 KB | None | 0 0
  1. import com.sun.mail.imap.IMAPStore;
  2. import org.junit.Test;
  3.  
  4. import javax.mail.*;
  5. import javax.mail.internet.MimeMessage;
  6. import java.util.Date;
  7. import java.util.Properties;
  8.  
  9. public class Main {
  10.  
  11.     @Test
  12.     public void imap() throws MessagingException {
  13.         Properties prop = System.getProperties();
  14.  
  15.         String host = "imap.gmail.com";
  16.  
  17.         String username = "user@gmail.com";
  18.         String password = "some-password";
  19.  
  20.         prop.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  21.         prop.setProperty("mail.imap.host", host);
  22.         prop.setProperty("mail.imap.port", "993");
  23.         prop.setProperty("mail.imap.starttls.enable", "true");
  24.         prop.setProperty("mail.imap.socketFactory.fallback", "false");
  25.         //prop.setProperty("mail.debug", "true");
  26.  
  27.         Session ses = Session.getInstance(prop, null);
  28.         Store store = ses.getStore("imap");
  29.         store.connect(host, username, password);
  30.  
  31.         if (!IMAPStore.class.isInstance(store))
  32.             throw new IllegalStateException("Is not IMAPStore");
  33.  
  34.         IMAPStore imapStore = (IMAPStore) store;
  35.         Folder defaultFolder = imapStore.getDefaultFolder();
  36.  
  37.         this.printFolderInfo(imapStore, defaultFolder, 1);
  38.     }
  39.  
  40.     private void printQuotaInfo(IMAPStore store, Folder folder) throws MessagingException {
  41.         Quota[] quotas;
  42.  
  43.         try {
  44.             quotas = store.getQuota(folder.getFullName());
  45.         } catch (MessagingException e) {
  46.             if (e.getMessage().contains(" NO [NONEXISTENT] Unknown folder:"))
  47.                 return;
  48.  
  49.             throw e;
  50.         }
  51.  
  52.         for (Quota quota : quotas) {
  53.             System.out.println(String.format("quotaRoot:'%s'", quota.quotaRoot));
  54.  
  55.             for (Quota.Resource resource : quota.resources) {
  56.                 System.out.println(String.format("name:'%s', limit:'%s', usage:'%s',",
  57.                         resource.name, resource.limit, resource.usage));
  58.             }
  59.         }
  60.     }
  61.  
  62.     private long printFolderInfo(IMAPStore store, Folder folder, int level) throws MessagingException {
  63.         long sizeSum = 0;
  64.         long sizeMessages = 0;
  65.  
  66.         try {
  67.             folder.open(Folder.READ_ONLY);
  68.  
  69.             for (Message message : folder.getMessages())
  70.                 sizeSum = sizeSum + message.getSize();
  71.  
  72.             folder.close(false);
  73.         } catch (MessagingException e) {
  74.             if (e.getMessage().compareTo("folder cannot contain messages") != 0)
  75.                 throw e;
  76.         }
  77.  
  78.         sizeMessages = sizeSum;
  79.  
  80.         for (Folder child : folder.list())
  81.             sizeSum = sizeSum + this.printFolderInfo(store, child, level + 1);
  82.  
  83.         System.out.println("---");
  84.         System.out.println(String.format("%s - folderFullName:'%s', folderSizeMessages:'%s', folderSizeSum:'%s'",
  85.                 level, folder.getFullName(), sizeMessages, sizeSum));
  86.  
  87.         this.printQuotaInfo(store, folder);
  88.  
  89.         return sizeSum;
  90.     }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement