// JavaMail Demo -- Williams
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

public class IMapMailDemo {
    public static void main(String args[]) throws Exception {
	String host = args[0];
	String username = args[1];
	String password = args[2];

	// Create empty properties
	Properties props = new Properties();
	
	// Get session
	Session session = Session.getDefaultInstance(props, null);

	// Get the store
	Store store = session.getStore("imap");
	store.connect(host, username, password);

	// Get folder
	Folder folder = store.getFolder("INBOX");
	folder.open(Folder.READ_ONLY);
	// print sub folders
	Folder [] folders = folder.list();
	if (folders.length==0) System.out.println("No sub folders");
	for (int ij=0;ij<folders.length;ij++) {
	    System.out.println("Subfolder: " + folders[ij].getFullName());
	}


	// Get directory
	Message message[] = folder.getMessages();
	if (message.length==0) System.out.println("No mail");
	// print first sender, subject, and message
	for (int i=0, n=message.length; i<n; i++) {
	    System.out.println(i + ": " + message[i].getFrom()[0] 
			       + "\t" + message[i].getSubject());
	    System.out.println(message[i].getContent().toString());
	}

	// Close connection 
	folder.close(false);
	store.close();
    }
}

