How can I use an Authenticator to prompt for username and password when reading mail from a IMAP/POP server?

John Zukowski

Keep in mind that the Authenticator in JavaMail is different than the one in the java.net package. To use the javax.mail.Authenticator, the basic process to connect to the Store is as follows:

// Create empty properties
Properties props = new Properties();
props.put("mail.host", host);

// Setup authentication, get session
Authenticator auth = new PopupAuthenticator();
Session session = Session.getInstance(
    props, auth);

// Get the store
Store store = session.getStore("pop3");
Then, you would need to create a PopupAuthenticator class that extends Authenticator. In the public PasswordAuthentication getPasswordAuthentication() method, the class would pop a frame up prompting for username and password, returning the information in a PasswordAuthentication object.