package ch.SWITCH.aai.imapauthentication; import java.security.Security; import java.util.Properties; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Store; import javax.servlet.ServletRequest; import edu.yale.its.tp.cas.auth.provider.WatchfulPasswordHandler; /** * @author poroli * * This class implements the IMAP authentication. */ public class ImapAuthentication extends WatchfulPasswordHandler { // host name here is the POP or IMAP server. private static final String host = "imap.switch.ch"; //provider can either be POP or IMAP private static final String provider = "imap"; public boolean authenticate(ServletRequest request, String username, String password) { // create the properties for the Session Properties props = new Properties(); // configure the jvm to use the jsse security. Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); // set this session up to use SSL for IMAP connections props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // don't fallback to normal IMAP connections on failure. props.setProperty("mail.imap.socketFactory.fallback", "false"); // use the simap port for imap/ssl connections. props.setProperty("mail.imap.socketFactory.port", "993");ImapAuthentication // after Session creation Session session = Session.getInstance(props); // after Session creation Store store = null; boolean authenticated = false; try { store = session.getStore(provider); store.connect(host, username, password); if (store.isConnected()) authenticated = true; } catch (MessagingException e) { e.printStackTrace(); } finally { try { store.close(); } catch (MessagingException e) { } } return authenticated; } }