001    
002    import java.util.Properties;
003    
004    import javax.mail.FetchProfile;
005    import javax.mail.Folder;
006    import javax.mail.Message;
007    import javax.mail.MessagingException;
008    import javax.mail.Session;
009    import javax.mail.Store;
010    
011    
012    public class Email {    
013    
014        public static void main(String[] args) {
015            String protocol = "imap";
016            String host = "cookie.csail.mit.edu";
017            String username = "6005";
018            String password = "6005";
019            
020            if (args.length != 0) {
021                protocol = args[0];
022                host = args[1];
023                username = args[2];
024            }
025            
026            if (protocol.equals("imaps") 
027                && host.equals("imap.csail.mit.edu")) {
028                // For CSAIL mail only: 
029                // CSAIL's certificate authority is stored in a file called "cacerts-plus-csail"
030                // assumed to be in the current directory.  Certificates can be added and
031                // removed from this file using the keytool command included with Java.
032                System.setProperty("javax.net.ssl.trustStore", "cacerts-plus-csail");
033                System.setProperty("javax.net.ssl.trustStorePassword", "changeit");            
034    
035                // If you're using MIT mail (or another mail system that has its
036                // certificates by one of the major certificate authorities, which
037                // Java already knows about), then you don't need the two lines above,
038                // and you can comment them out.
039            }
040    
041            try {
042                // Create a session and a store
043                Properties props = new Properties();
044                Session session = Session.getDefaultInstance(props, null);
045                Store store = session.getStore(protocol);
046        
047                // Connect to the server
048                System.err.println("Connecting...");
049                store.connect(host, username, password);        
050                System.err.println("Connected to " + host);
051        
052                // Display folder names and message counts
053                Folder[] folders = store.getDefaultFolder().list("*");
054                for (Folder f : folders) {
055                    System.out.println(f.getMessageCount() + "\t" + f.getFullName());
056                }
057                
058                // Close the connection
059                store.close();
060                
061            } catch (MessagingException e) {
062                throw new RuntimeException(e);
063            }
064        }
065    }