Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.mail.imap.IMAPStore;
- import org.junit.Test;
- import javax.mail.*;
- import javax.mail.internet.MimeMessage;
- import java.util.Date;
- import java.util.Properties;
- public class Main {
- @Test
- public void imap() throws MessagingException {
- Properties prop = System.getProperties();
- String host = "imap.gmail.com";
- String username = "user@gmail.com";
- String password = "some-password";
- prop.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- prop.setProperty("mail.imap.host", host);
- prop.setProperty("mail.imap.port", "993");
- prop.setProperty("mail.imap.starttls.enable", "true");
- prop.setProperty("mail.imap.socketFactory.fallback", "false");
- //prop.setProperty("mail.debug", "true");
- Session ses = Session.getInstance(prop, null);
- Store store = ses.getStore("imap");
- store.connect(host, username, password);
- if (!IMAPStore.class.isInstance(store))
- throw new IllegalStateException("Is not IMAPStore");
- IMAPStore imapStore = (IMAPStore) store;
- Folder defaultFolder = imapStore.getDefaultFolder();
- this.printFolderInfo(imapStore, defaultFolder, 1);
- }
- private void printQuotaInfo(IMAPStore store, Folder folder) throws MessagingException {
- Quota[] quotas;
- try {
- quotas = store.getQuota(folder.getFullName());
- } catch (MessagingException e) {
- if (e.getMessage().contains(" NO [NONEXISTENT] Unknown folder:"))
- return;
- throw e;
- }
- for (Quota quota : quotas) {
- System.out.println(String.format("quotaRoot:'%s'", quota.quotaRoot));
- for (Quota.Resource resource : quota.resources) {
- System.out.println(String.format("name:'%s', limit:'%s', usage:'%s',",
- resource.name, resource.limit, resource.usage));
- }
- }
- }
- private long printFolderInfo(IMAPStore store, Folder folder, int level) throws MessagingException {
- long sizeSum = 0;
- long sizeMessages = 0;
- try {
- folder.open(Folder.READ_ONLY);
- for (Message message : folder.getMessages())
- sizeSum = sizeSum + message.getSize();
- folder.close(false);
- } catch (MessagingException e) {
- if (e.getMessage().compareTo("folder cannot contain messages") != 0)
- throw e;
- }
- sizeMessages = sizeSum;
- for (Folder child : folder.list())
- sizeSum = sizeSum + this.printFolderInfo(store, child, level + 1);
- System.out.println("---");
- System.out.println(String.format("%s - folderFullName:'%s', folderSizeMessages:'%s', folderSizeSum:'%s'",
- level, folder.getFullName(), sizeMessages, sizeSum));
- this.printQuotaInfo(store, folder);
- return sizeSum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement