Customizing the JavaMail Authenticator

Subclass javax.mail.Authenticator and provide an instance of this class when you create the session. The callback method getPasswordAuthentication is called whenever it is necessary to prompt for username/password.

This example provides an example in Swing using POP3.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.mail.internet.*;
import javax.mail.*;
import java.util.*;
 
public class Main
{
   public static void main(String []args) {
      try {
         String host = "POP3 HOST HERE";
   
         Properties properties = new Properties();
         properties.put("mail.host", host);
         Authenticator authenticator = new WindowAuthenticator();
         
         Session session = Session.getInstance(properties, authenticator);
  
         Store store = session.getStore("pop3");
         System.out.println("connecting tot POP3 server...");
         store.connect();
  
         // only "INBOX" is supported
         Folder folder = store.getFolder("INBOX");
 
         // open the folder read-only
         folder.open(Folder.READ_ONLY);
 
         System.out.println("Getting messages...");
         // get all the messages and print from and subject field 
         Message messages[] = folder.getMessages();
         for (int i=0; i<messages.length; i++) {
            System.out.print(messages[i].getMessageNumber() + "t");
            System.out.print(messages[i].getSentDate() + "t");
            System.out.print(messages[i].getFrom()[0] + "t");
            System.out.println(messages[i].getSubject());
         }
 
         // close the folder, but do not delete (expunge) the messages
         folder.close(false);
         store.close();
 
         System.exit(0);
      }
      catch(Exception e) {
         e.printStackTrace();
      }
   }
}
 
class WindowAuthenticator extends Authenticator
{
   protected PasswordAuthentication getPasswordAuthentication() {
      WindowAuthenticatorDialog dialog = new WindowAuthenticatorDialog();
      dialog.pack();
      dialog.setResizable(false);
      dialog.setVisible(true);
 
      if (dialog.isCancelled()) {
         return null;
      }
 
      System.out.println("Username supplied: " + dialog.getUsername());
      System.out.println("Password supplied: " + dialog.getPassword());
 
      return new PasswordAuthentication(dialog.getUsername(), dialog.getPassword());
   }
}
 
class WindowAuthenticatorDialog extends JDialog
{
   private JTextField usernameTextField = new JTextField(20);
   private JTextField passwordTextField = new JTextField(20);
   private boolean cancelled = false;
 
   public WindowAuthenticatorDialog() {
      // make modal
      super(new JFrame(), "Supply your POP3 username and password", true);
 
      getContentPane().setLayout(new GridLayout(3, 1, 10, 10));
 
      JPanel panel1 = new JPanel(new FlowLayout());
      panel1.add(new JLabel("Username:"));
      panel1.add(usernameTextField);
      JPanel panel2 = new JPanel(new FlowLayout());
      panel2.add(new JLabel("Password:"));
      panel2.add(passwordTextField);
      JPanel panel3 = new JPanel(new FlowLayout());
      JButton okButton = new JButton("Ok");
      JButton cancelButton = new JButton("Cancel");
      panel3.add(okButton);
      panel3.add(cancelButton);
 
      getContentPane().add(panel1);
      getContentPane().add(panel2);
      getContentPane().add(panel3);
 
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      int x = (int) ((d.getWidth() - getWidth()) / 2);
      int y = (int) ((d.getHeight() - getHeight()) / 2);
      setLocation(x, y);
 
      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            setVisible(false);
         }
      });
 
      cancelButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            cancelled = true;
            setVisible(false);
         }
      });
   }
 
   public String getUsername() {
      return usernameTextField.getText();
   }
 
   public String getPassword() {
      return passwordTextField.getText();
   }
 
   public boolean isCancelled() {
      return false;
   }
}